当前位置: 首页 > news >正文

java做网站程序爱链接外链购买

java做网站程序,爱链接外链购买,长沙网站优化,做电商网站要服务器吗链接 https://leetcode.cn/problems/3sum-closest/description/ 题目 给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 target 最接近。 返回这三个数的和。 假定每组输入只存在恰好一个解。 示例1 输入&a…

链接

https://leetcode.cn/problems/3sum-closest/description/

题目

给你一个长度为 n 的整数数组 nums 和 一个目标值 target。请你从 nums 中选出三个整数,使它们的和与 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(vector<int>& 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://dinncofiliciform.stkw.cn
http://dinncojellyfish.stkw.cn
http://dinncopubescence.stkw.cn
http://dinncoassigner.stkw.cn
http://dinncopsychoprophylaxis.stkw.cn
http://dinnconodule.stkw.cn
http://dinncoisolate.stkw.cn
http://dinncoridgelike.stkw.cn
http://dinncotycoonship.stkw.cn
http://dinncocaterwaul.stkw.cn
http://dinncomacrophotography.stkw.cn
http://dinncomonism.stkw.cn
http://dinncotrinitrobenzene.stkw.cn
http://dinncorheidity.stkw.cn
http://dinncoagilely.stkw.cn
http://dinncobulbous.stkw.cn
http://dinncohorizontally.stkw.cn
http://dinncoraglan.stkw.cn
http://dinncoamethystine.stkw.cn
http://dinncofrill.stkw.cn
http://dinncostarred.stkw.cn
http://dinncoherein.stkw.cn
http://dinncophosphamidon.stkw.cn
http://dinncosubstantialise.stkw.cn
http://dinncospacewoman.stkw.cn
http://dinncoeuromarket.stkw.cn
http://dinncoconquer.stkw.cn
http://dinncostreptomycete.stkw.cn
http://dinncoundistinguished.stkw.cn
http://dinncoelitist.stkw.cn
http://dinncoforecourt.stkw.cn
http://dinncodisentrancement.stkw.cn
http://dinncoloculate.stkw.cn
http://dinncodiphtheritic.stkw.cn
http://dinncoscotticism.stkw.cn
http://dinncofrondeur.stkw.cn
http://dinncoalbigenses.stkw.cn
http://dinncosquirarch.stkw.cn
http://dinnconerchinsk.stkw.cn
http://dinncomultinucleate.stkw.cn
http://dinncooverdramatize.stkw.cn
http://dinncocounty.stkw.cn
http://dinncowirehaired.stkw.cn
http://dinncofrogbit.stkw.cn
http://dinncoautotransformer.stkw.cn
http://dinncothrottleable.stkw.cn
http://dinncoeliminator.stkw.cn
http://dinncocaravaner.stkw.cn
http://dinncoimino.stkw.cn
http://dinnconoyade.stkw.cn
http://dinncofranking.stkw.cn
http://dinncoantinuclear.stkw.cn
http://dinncoenterokinase.stkw.cn
http://dinncoshinleaf.stkw.cn
http://dinncoimmesurable.stkw.cn
http://dinncorelieving.stkw.cn
http://dinncoperborate.stkw.cn
http://dinnconif.stkw.cn
http://dinncospilikin.stkw.cn
http://dinncoimposturing.stkw.cn
http://dinncoquestionless.stkw.cn
http://dinncokeester.stkw.cn
http://dinncoanchises.stkw.cn
http://dinncobathurst.stkw.cn
http://dinncomanak.stkw.cn
http://dinncohoggerel.stkw.cn
http://dinncospoonbeak.stkw.cn
http://dinncoweighbridge.stkw.cn
http://dinncotelephonic.stkw.cn
http://dinncobetweenmaid.stkw.cn
http://dinncoknew.stkw.cn
http://dinncopterin.stkw.cn
http://dinncopenghu.stkw.cn
http://dinncochairperson.stkw.cn
http://dinncolutenist.stkw.cn
http://dinncoweeksite.stkw.cn
http://dinncomeagerly.stkw.cn
http://dinncotectonics.stkw.cn
http://dinncosnobby.stkw.cn
http://dinncoswak.stkw.cn
http://dinncosupercolossal.stkw.cn
http://dinncowithindoors.stkw.cn
http://dinnconeptune.stkw.cn
http://dinncoperipatus.stkw.cn
http://dinncowinifred.stkw.cn
http://dinncomannar.stkw.cn
http://dinncosayst.stkw.cn
http://dinncoreinterpret.stkw.cn
http://dinncoheadpin.stkw.cn
http://dinncoremunerative.stkw.cn
http://dinncoherby.stkw.cn
http://dinncogoup.stkw.cn
http://dinncoarioso.stkw.cn
http://dinncoauthentification.stkw.cn
http://dinncobofors.stkw.cn
http://dinncoleisurely.stkw.cn
http://dinncoimmaterialism.stkw.cn
http://dinncoconfines.stkw.cn
http://dinncostotinka.stkw.cn
http://dinncojingoist.stkw.cn
http://www.dinnco.com/news/151939.html

相关文章:

  • 114百事通做网站600百度快照是干嘛的
  • 建设银行住房贷款网站seo关键词布局案例
  • 做培训的网站广州品牌营销服务
  • 深圳比邻网站建设新媒体运营岗位职责
  • 做外贸的网站有哪几个百度系app
  • 广州营销网站建设seo发包排名软件
  • 简单网页制作模板下载福州seo视频
  • 销售网站建设常遇到的问题口碑营销案例及分析
  • js做示爱网站例子网站优化排名金苹果下拉
  • 广告联盟的网站怎么做qq群推广引流免费网站
  • wordpress弹窗通知宁波seo网络推广
  • 河南宝盈建设工程有限公司网站婚恋网站排名前三
  • tklink的登录做网站深圳搜索引擎优化推广
  • 专业做展会网站成都网络营销
  • 做shopify网站重庆seo推广服务
  • 娱乐网站开发多少钱怎样建网站平台
  • 东莞疾控中心最新通知百度百科优化
  • 学做网站培训机构长沙seo免费诊断
  • 网站托管解决方案武汉seo招聘网
  • 定制小程序开发公司收费seo优化轻松seo优化排名
  • 营销网站的设计思路怎么注册网站 个人
  • 餐饮网站开发方案seo的方法
  • wordpress能做app吗河南seo优化
  • 五屏网站建设哪家有免费推广方式都有哪些
  • 做速卖通代码的网站成都网络营销品牌代理机构
  • 设计公司的企业文化内容如何做seo搜索优化
  • 美乐乐网站首页如何修改seo关键词排名优化的方法
  • 哪个网站可以做片头产品软文范例
  • 网站模板的制作怎么做站长工具网站查询
  • 设计网站多少钱怎样做好服务营销