class Solution {
public:
void quick_order(vector<int>& num, int star, int en)//快排
{
int start = star;
int end = en;
if (start >= end)
return; int index = num[start];//就第一个设为阈值
while (start < end)
{
while (start < end && index <= num[end])//先看右边,把第一个小于index数找出
end--; int temp1 = num[start];//交换
num[start] = num[end];
num[end] = temp1; while (start < end && index >= num[start])//再看右边,把第一个大于index的数找出
start++; int temp2 = num[start];//交换
num[start] = num[end];
num[end] = temp2;
} quick_order(num, star, start-);//分左右子集迭代
quick_order(num, start + , en);
return;
} int threeSumClosest(vector<int>& nums, int target) {
int len=nums.size();
long res=*INT_MAX; if(len==)//输入判断
return ; int start=,end=len-;
quick_order(nums,start,end);//快排 for (int c = nums.size()-; c >= ; --c)
{
int tar=target-nums[c];//设置a和b要找的数字
for (int a = , b = c-; a < b; )
{
if (abs(res)>=abs(nums[a]+nums[b]+nums[c]-target))//先检验是否更接近target
res=nums[a]+nums[b]+nums[c]-target; int tmp_sum = nums[a]+nums[b];//再进行下一步迭代
if (tmp_sum < tar)
++a;
else
--b;
}
}
return int(res+target); }
};

分析:

和上个类似,也是O(n^2)时间复杂度遍历。

leecode第十六题(最接近的三数之和)的更多相关文章

  1. Leetcode题库——16.最接近的三数之和

    @author: ZZQ @software: PyCharm @file: threeSumClosest.py @time: 2018/10/14 20:28 说明:最接近的三数之和. 给定一个包 ...

  2. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  3. lintcode-59-最接近的三数之和

    59-最接近的三数之和 给一个包含 n 个整数的数组 S, 找到和与给定整数 target 最接近的三元组,返回这三个数的和. 注意事项 只需要返回三元组之和,无需返回三元组本身 样例 例如 S = ...

  4. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  5. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  6. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  7. LeetCode-016-最接近的三数之和

    最接近的三数之和 题目描述:给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只 ...

  8. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  9. LeetCode 16. 3Sum Closest. (最接近的三数之和)

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  10. 最接近的三数之和(给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数, 使得它们的和与 target 最接近。返回这三个数的和)

    例如,给定数组 nums = [-1,2,1,-4], 和 target = 1. 与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2). 思路:首先对数组进行排序     ...

随机推荐

  1. apache编译安装php后需要注意以下配置

    安装后, 编辑apache配置文件 vi /usr/local/apache2/conf/httpd.conf 可以看到 LoadModule php7_module modules/libphp7. ...

  2. ManyToManyField 增加记录

    class BOMView(View): def get(self,request): obj=BOMForm() return render(request,'bom.html',{'obj':ob ...

  3. P3332 [ZJOI2013]K大数查询(线段树套线段树+标记永久化)

    P3332 [ZJOI2013]K大数查询 权值线段树套区间线段树 把插入的值离散化一下开个线段树 蓝后每个节点开个线段树,维护一下每个数出现的区间和次数 为了防止MLE动态开点就好辣 重点是标记永久 ...

  4. Django之URL控制器(路由层)

    url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), 一.视图层路由配置系统 URL配置(URLconf)就像Django ...

  5. linux中没有tree命令,command not found,解决办法

    在有网络的情况下: 1.包管理器安装 centos 中用  yum -y install tree ubuntu 中用  apt-get install tree 当然如果需要权限不要忘了在前面加上 ...

  6. ubuntu上解决访问github慢的方法

    1.进入终端命令行模式,输入sudo vi /etc/hosts 2.输入i进入编辑命令,英文输入法输入G,vim编辑器跳到hosts文件的最后一行 3.用浏览器访问 IPAddress.com 使用 ...

  7. 集合框架-Map集合

    * Map集合和Collection集合的区别? * Map集合存储元素是成对出现的,Map集合的键是唯一的,值是可重复的.可以把这个理解为:夫妻对 * Collection集合存储元素是单独出现的, ...

  8. JS实现页面字体繁简转换

    封装的JS代码 // 网页简繁体转换 // 本js用于客户在网站页面选择繁体中文或简体中文显示,默认是正常显示,即简繁体同时显示 // 在用户第一次访问网页时,会自动检测客户端语言进行操作并提示.此功 ...

  9. 【python002-设计小游戏】

    用python设计第一个游戏 一.运行idle的快捷方式:F5 二.首先设计一款小游戏: print('-------开启python学习之旅--------')temp = input(" ...

  10. Python 数据分析 - 索引和选择数据

    loc,iloc,ix三者间的区别和联系 loc .loc is primarily label based, but may also be used with a boolean array. 就 ...