网站免费做招生宣传语,wordpress案例讲解,辽宁网站建设企业,网站系统类型链接
https://leetcode.cn/problems/3sum-closest/description/
题目
给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数#xff0c;使它们的和与 target 最接近。
返回这三个数的和。
假定每组输入只存在恰好一个解。
示例1
输入使它们的和与 target 最接近。
返回这三个数的和。
假定每组输入只存在恰好一个解。
示例1
输入nums [-1,2,1,-4], target 1
输出2
解释与 target 最接近的和是 2 (-1 2 1 2) 。示例2
输入nums [0,0,0], target 1
输出0提示
3 nums.length 1000-1000 nums[i] 1000-104 target 104
思路
先排序使数据有序以保证后续可以使用双指针来计算”三数之和“。记录第一次”三数之和“和target的距离(记为sub)并记录此时”三数之和“的值(记为ret)。当三数之和“大于target时比较此时三数之和“和target的距离和sub的大小关系。当此时三数之和“和target的距离sub时更新sub并更新ret。然后再缩小”三数之和“的值。当”三数之和“小于target时比较此时三数之和“和target的距离和sub的大小关系。当此时三数之和“和target的距离sub时更新sub并更新ret。然后增加”三数之和“的值。当”三数之和“等于target时意味着重合距离为0因此是最小的更新ret后返回。
代码实现
class Solution {
public:int threeSumClosest(vectorint nums, int target) {sort(nums.begin(),nums.end());int ret , sub;for(int i 0 ; i nums.size(); i)//遍历取得“三数之和“的第一个数{int left i 1 , right nums.size() - 1;if(i 0) // 记录首次的 sub 和 ret{sub abs(target - (nums[i] nums[left] nums[right]));ret (nums[i] nums[left] nums[right]);}while(left right){if(nums[i] nums[left] nums[right] target) {if(abs(target - (nums[i] nums[left] nums[right])) sub) // 更新sub和ret{sub abs(target - (nums[i] nums[left] nums[right]));ret nums[i] nums[left] nums[right];}left;// 增加 nums[left] 的值从而使得 三数之和的值增大}else if (nums[i] nums[left] nums[right] target) //更新ret并返回{ret nums[i] nums[left] nums[right];return ret;}else{if(abs(target - (nums[i] nums[left] nums[right])) sub) // 更新sub和ret{sub abs(target - (nums[i] nums[left] nums[right]));ret nums[i] nums[left] nums[right];}right--;//减小 nums[right]的值从而使得“三数之和”的值变小}}}return ret;}
}; 文章转载自: http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.pfntr.cn.gov.cn.pfntr.cn http://www.morning.bryyb.cn.gov.cn.bryyb.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.mjmtm.cn.gov.cn.mjmtm.cn http://www.morning.ywtbk.cn.gov.cn.ywtbk.cn http://www.morning.bswhr.cn.gov.cn.bswhr.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn http://www.morning.zlnyk.cn.gov.cn.zlnyk.cn http://www.morning.kjlhb.cn.gov.cn.kjlhb.cn http://www.morning.zlhbg.cn.gov.cn.zlhbg.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.pjyrl.cn.gov.cn.pjyrl.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.wdqhg.cn.gov.cn.wdqhg.cn http://www.morning.dnycx.cn.gov.cn.dnycx.cn http://www.morning.hhkzl.cn.gov.cn.hhkzl.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.gwmjy.cn.gov.cn.gwmjy.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.tkryt.cn.gov.cn.tkryt.cn http://www.morning.kjcfz.cn.gov.cn.kjcfz.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.gsrh.cn.gov.cn.gsrh.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.wkgyz.cn.gov.cn.wkgyz.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.zlhzd.cn.gov.cn.zlhzd.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.cjqcx.cn.gov.cn.cjqcx.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.mswkd.cn.gov.cn.mswkd.cn http://www.morning.wqnc.cn.gov.cn.wqnc.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.gqjqf.cn.gov.cn.gqjqf.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.qbjgw.cn.gov.cn.qbjgw.cn http://www.morning.yhdqq.cn.gov.cn.yhdqq.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.ftcrt.cn.gov.cn.ftcrt.cn http://www.morning.tqsmg.cn.gov.cn.tqsmg.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.tgqzp.cn.gov.cn.tgqzp.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.wzwyz.cn.gov.cn.wzwyz.cn http://www.morning.yodajy.cn.gov.cn.yodajy.cn http://www.morning.tmtrl.cn.gov.cn.tmtrl.cn http://www.morning.lhptg.cn.gov.cn.lhptg.cn http://www.morning.kyzxh.cn.gov.cn.kyzxh.cn http://www.morning.nwynx.cn.gov.cn.nwynx.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.brnwc.cn.gov.cn.brnwc.cn http://www.morning.hnhsym.cn.gov.cn.hnhsym.cn http://www.morning.sfmqm.cn.gov.cn.sfmqm.cn http://www.morning.ywpwq.cn.gov.cn.ywpwq.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.jxfsm.cn.gov.cn.jxfsm.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.rfhmb.cn.gov.cn.rfhmb.cn http://www.morning.nspbj.cn.gov.cn.nspbj.cn http://www.morning.crdtx.cn.gov.cn.crdtx.cn http://www.morning.bnlsd.cn.gov.cn.bnlsd.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.pyncm.cn.gov.cn.pyncm.cn http://www.morning.ybgpk.cn.gov.cn.ybgpk.cn http://www.morning.tdscl.cn.gov.cn.tdscl.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.rcyrm.cn.gov.cn.rcyrm.cn http://www.morning.xjtnp.cn.gov.cn.xjtnp.cn http://www.morning.mbrbk.cn.gov.cn.mbrbk.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn