1. class Solution {
  2. public:
  3. void quick_order(vector<int>& num, int star, int en)//快排
  4. {
  5. int start = star;
  6. int end = en;
  7. if (start >= end)
  8. return;
  9.  
  10. int index = num[start];//就第一个设为阈值
  11. while (start < end)
  12. {
  13. while (start < end && index <= num[end])//先看右边,把第一个小于index数找出
  14. end--;
  15.  
  16. int temp1 = num[start];//交换
  17. num[start] = num[end];
  18. num[end] = temp1;
  19.  
  20. while (start < end && index >= num[start])//再看右边,把第一个大于index的数找出
  21. start++;
  22.  
  23. int temp2 = num[start];//交换
  24. num[start] = num[end];
  25. num[end] = temp2;
  26. }
  27.  
  28. quick_order(num, star, start-);//分左右子集迭代
  29. quick_order(num, start + , en);
  30. return;
  31. }
  32.  
  33. int threeSumClosest(vector<int>& nums, int target) {
  34. int len=nums.size();
  35. long res=*INT_MAX;
  36.  
  37. if(len==)//输入判断
  38. return ;
  39.  
  40. int start=,end=len-;
  41. quick_order(nums,start,end);//快排
  42.  
  43. for (int c = nums.size()-; c >= ; --c)
  44. {
  45. int tar=target-nums[c];//设置a和b要找的数字
  46. for (int a = , b = c-; a < b; )
  47. {
  48. if (abs(res)>=abs(nums[a]+nums[b]+nums[c]-target))//先检验是否更接近target
  49. res=nums[a]+nums[b]+nums[c]-target;
  50.  
  51. int tmp_sum = nums[a]+nums[b];//再进行下一步迭代
  52. if (tmp_sum < tar)
  53. ++a;
  54. else
  55. --b;
  56. }
  57. }
  58. return int(res+target);
  59.  
  60. }
  61. };

分析:

和上个类似,也是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的rewrite机制配置

    步骤: 1:启用rewrite模块,在默认情况下,没有启用 修改httpd.conf文件 #启动rewrite模块 LoadModule rewrite_module modules/mod_rewr ...

  2. Selenium在Firefox中踩过的

    本文转至 http://www.51testing.com/html/11/n-3711311.html,作者对webdriver在Firefox中设置profile配置项挺熟的,是用Python实现 ...

  3. Centos环境自写脚本查看使用php或nginx占用内存

    在CentOs6.4下,用root权限测试. # cd ~ //进入home目录 # vim .bashrc //编辑文件,把下面代码放入地址 mem () { top -n1 -b | head - ...

  4. Fiddler(三)Fiddler 报错creation of the root certificate was not successful

    打开CMD,找到Fiddler所在目录,我这里是把汉化版的解压在了桌面,所以通过CMD进入桌面下的Fiddler文件夹. 执行命令 makecert.exe -r -ss my -n -h -cy a ...

  5. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  6. Bayesian Program Synthesis - 初步探索

  7. 前端 --- 2 css

    一. CSS的几种引入方式 1.行内样式 2.内部样式 写在网页的<head></head>标签对的<style></style>标签对中 3.外部样式 ...

  8. python---01.各类计算机语言,python历史,变量,常量,数据类型,if条件

    一.认识计算机 1.硬件组成: CPU(大脑)  + 内存(缓冲) + 主板(连接各部分) + 电源(心脏)      + 显示器 + 键盘 +鼠标+ 显卡 + 硬盘 2.操作系统 ①windows  ...

  9. springboot项目打包部署在指定的tomcat容器中

    1.首先需要修改项目的打包方式,将package改为war <packaging>war</packaging> 2.移除spring boot web中的嵌入式tomcat ...

  10. Python3 tkinter基础 Canvas delete 删除画布中的所有图形

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...