16. 3Sum Closest -Medium

descrition

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.


  1. For example, given array S = {-1 2 1 -4}, and target = 1.
  2. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

解析

与 3Sum 的思路一样。不同在于,我们现在希望找到距离 target 最近的数,参看代码。

code


  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <limits>
  5. using namespace std;
  6. class Solution{
  7. public:
  8. int threeSumClosest(vector<int>& nums, int target){
  9. sort(nums.begin(), nums.end()); // ascending
  10. int min_gab = numeric_limits<int>::max();
  11. int ans = target;
  12. for(int i=0; i<nums.size(); i++){
  13. int target_local = target - nums[i];
  14. int ileft = i + 1;
  15. int iright = nums.size() - 1;
  16. while(ileft < iright){ // two pointer searching
  17. int sum = nums[ileft] + nums[iright];
  18. if(sum == target_local) // right answer
  19. return target;
  20. if(sum < target_local) // move ileft to increase sum
  21. ileft++;
  22. else // sum > target_local
  23. iright--;
  24. int gab = abs(sum - target_local);
  25. if(gab < min_gab){
  26. ans = sum + nums[i];
  27. min_gab = gab;
  28. }
  29. }
  30. }
  31. return ans;
  32. }
  33. };
  34. int main()
  35. {
  36. return 0;
  37. }

[array] leetCode-16. 3Sum Closest -Medium的更多相关文章

  1. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

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

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

  3. 蜗牛慢慢爬 LeetCode 16. 3Sum Closest [Difficulty: Medium]

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

  4. Array + two points leetcode.16 - 3Sum Closest

    题面 Given an array nums of n integers and an integer target, find three integers in nums such that th ...

  5. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  6. 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 ...

  7. Java [leetcode 16] 3Sum Closest

    题目描述: Given an array S of n integers, find three integers in S such that the sum is closest to a giv ...

  8. [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 ...

  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. [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 ...

随机推荐

  1. 为root账户更名

    为root账户更名 处于安全考虑许多管理员想把root更名,具体方法如下: 1.先以root登陆系统 2.用vi 编辑/etc/passwd文件,将第一行的第一个root修改为你想要的账户名,然后保存 ...

  2. Install Docker Mac OS X

    检查 Mac OS version 要求必须是 OS X 10.6 Snow Leopard or newer to run Boot2Docker 安装 Boot2Docker 列表内容 下载地址: ...

  3. 3. CONFIGURATION官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 3. CONFIGURATION 3.1 Broker Configs 3.2 Pr ...

  4. 可重入锁ReentrantLock--转载

    突然被问到什么是可重入锁?脑袋里闪过了n中概念,最终没有找到,从网上学习一下. 原文地址:https://www.ibm.com/developerworks/cn/java/j-jtp10264/ ...

  5. 借Stunnel工具保护E-mail服务器

    借Stunnel工具保护E-mail服务器 650) this.width=650;" onclick='window.open("http://blog.51cto.com/vi ...

  6. DG观察日志传输

    --primary端查询v$archived_log视图,确认日志是否被应用:   set lines 300 pages 300 col name for a20 select name,dest_ ...

  7. Java Servlet学习笔记(四)Servlet客户端Http请求

    Servlet 客户端 HTTP 请求 当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的.您可以查看 HTTP ...

  8. [React] Call setState with null to Avoid Triggering an Update in React 16

    Sometimes it’s desired to decide within an updater function if an update to re-render should be trig ...

  9. 请使劲回答一个关于UNIX/Linux自己主动扩展stack的问题

    有本事就出来,没本事就当鳖! 假设让我回答关于进程栈,线程栈的问题,仅仅要问题不笼统,仅仅要问题明白.我会一五一十地回答,正确率上九成,然而,可悲的是,问题往往他妈的都不是非常明白,因此,游戏到此结束 ...

  10. progerssbar-style 属性分析

    先看如下代码 <ProgressBar android:id="@+id/stateProgressBar" android:orientation="horizo ...