Question

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

Solution

  • 先排序,然后遍历每一个元素nums[i],并且在其之后找nums[j],nums[k],j<k,使得结果最接近。
  1. sum = nums[i] + nums[j] + nums[k];
  2. if sum > target k--;
  3. if sum < target j++;
  4. if sum == target return;

Code

  1. class Solution {
  2. public:
  3. int threeSumClosest(vector<int>& nums, int target) {
  4. sort(nums.begin(), nums.end());
  5. int minValue = nums[0] + nums[1] + nums[2];
  6. int res = abs(minValue - target);
  7. for (int i = 0; i < nums.size() - 2; i++) {
  8. int j = i + 1;
  9. int k = nums.size() - 1;
  10. while (j < k) {
  11. int sum = nums[i] + nums[j] + nums[k];
  12. if (abs(sum - target) < res) {
  13. res = abs(sum - target);
  14. minValue = sum;
  15. }
  16. if (sum > target)
  17. k--;
  18. else if (sum < target)
  19. j++;
  20. else
  21. return sum;
  22. }
  23. }
  24. return minValue;
  25. }
  26. };

LeetCode——3Sum Closest的更多相关文章

  1. [LeetCode]3Sum Closest题解

    3sum Closest: Given an array S of n integers, find three integers in S such that the sum is closest ...

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

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

  3. Leetcode 3Sum Closest

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

  4. LeetCode 3Sum Closest (Two pointers)

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

  5. leetcode 3Sum Closest python

    class Solution(object): def threeSumClosest(self, nums, target): """ :type nums: List ...

  6. LeetCode 3Sum Closest 最近似的3sum(2sum方法)

    题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...

  7. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  8. LeetCode之“散列表”:Two Sum && 3Sum && 3Sum Closest && 4Sum

    1. Two Sum 题目链接 题目要求: Given an array of integers, find two numbers such that they add up to a specif ...

  9. 3Sum Closest - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 3Sum Closest - LeetCode 注意点 和3Sum那道题的target是0,这道题是题目给定的 要先计算误差再移动指针 解法 解法一:做法 ...

随机推荐

  1. Nginx无法启动,80端口被PID=4占用

    在nginx启动后,error.log中总是显示 80 端口被占用. 通过netstat -ano发现,其被一个叫PID=4的系统服务占用. 网上大多数的方法是说通过regidit修改注册表的方式解决 ...

  2. Xshell 连接虚拟机特别慢 解决方案

    由于各种原因,xshell连接虚拟机的rhel或者CentOS都几乎是龟速...... 今天专门查了一下解决方案: 原来是ssh的服务端在连接时会自动检测dns环境是否一致导致的,修改为不检测即可,操 ...

  3. Sublime Text 中文

    1.打开Sublime Text 2.Ctrl+Shift+P,输入Package Control: Install Package回车 3.输入LocalizedMenu,回车 4.点击菜单help ...

  4. linux下dubbo调试 ---telnet命令

    linux下启动dubbo服务端, 怎么调试? 方法有二: 1. 自己写简单消费者功能,进行各种情况测试.(这确实是有必要的) 2. 使用telnet直接连接上dubbo,使用命令调用,然后调试.(这 ...

  5. SAP Idoc 事务码

    SALE Display ALE Customizing SM59 RFC Destinations (Display/Maintain) BD64 Maintenance of Distributi ...

  6. Linux常见系统命令与文件操作

    一.Linux常见文件操作 (1)cd /: 在Linux 系统中斜杠“/”表示的是根目录. cd / ,即进入根目录. (2)cd ~命令是,进入用户在该系统的home目录,例如xz用户,则进入/r ...

  7. 字符串的partition函数

    partition函数 str1='sdga2a34'aa=str1.partition('a') print(aa) """ ('sdg', 'a', '2a34') ...

  8. JQuery变量数字相加的研究

    在 jquery中,一个变量和一个数字相加,期望得到1+2=3但是: eg: <input type="text" class="input_Num" i ...

  9. mysql锁机制之间隙锁(Next-Key锁)(五)

    间隙锁(Next-Key锁) 当我们用范围条件而不是相等条件检索数据,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录的 索引项加锁:对于键值在条件范围内但并不存在的记录,叫做“间隙(GA ...

  10. SpringCloud Config Server中{application}等占位符使用场景设置默认拉去分支

    Spring Cloud Config服务器支持一个Git仓库URL,其中包含{application}和{profile}(以及{label})的占位符. 1.各个占位符所代表的含义 applica ...