LeetCode: 3SumClosest

Title :

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.

    For example, given array S = {-1 2 1 -4}, and target = 1.

    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
int threeSumClosest(vector<int> &num, int target){
        sort(num.begin(),num.end());
        int min = INT_MAX;
        int result;
        for (int i = 0 ; i < num.size()-2; i++){
            if (i > 0 && num[i] == num[i-1])
                continue;
            int left = i+1;
            int right = num.size()-1;
            int goal = target - num[i];
            while (left < right){
                int diff = abs(target - num[left] - num[right] - num[i]);
                if (diff < min){
                    min = diff;
                    result = num[left] + num[right] + num[i];
                }
                if (num[left] + num[right] < goal){
                    while (left < right && (num[left] == num[left+1]))
                        left++;
                    left++;
                }else if (num[left] + num[right] > goal){
                    while (left < right && (num[right] == num[right-1]))
                        right--;
                    right--;
                }else{
                    return target;
                }
            }
        }
        return result;
    }
时间: 2024-07-29 12:31:57

LeetCode: 3SumClosest的相关文章

leetcode: 2Sum/3Sum/3SumClosest/4Sum系列问题(转载)

转载:http://blog.csdn.net/li4951/article/details/8693212 leetcode上有好几道关于数组中几个数据和为target的题目.恰好正在看剑指offer中"和为s的两个数组这章",据此思想,leetcode上的三道题目都被我解决了.总结一下. 1.twoSum: 输入一个递增数组和一个数字s,在数组中查找两个数使得它们的和正好是s. 既然题目中已经提到了"递增数组",那么肯定不会暴力了.因此肯定有<O(n*n)

leetcode 日记 3sumclosest java

整体思路为将threeSum将为twoSum即可 public int solution(int[] nums, int target) { if (nums.length == 3) { return nums[0] + nums[1] + nums[2]; } else { Arrays.sort(nums); int r = 10000;//此两处10000为省事而设,如果严谨应该大致找到其中的一个较大距离 int distance = 10000; for (int i = 0; i <

LeetCode之3SumClosest

题目: 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. For example, given array S = {-

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

[LeetCode][JavaScript]3Sum Closest

3Sum Closest 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. For example, given arr

[leetcode]3Sum Closest @ Python

原题地址:http://oj.leetcode.com/problems/3sum-closest/ 题意:数组中每三个元素进行求和,找出所有和中大小最接近target的和,并返回这个和与target之间的差值. 解题思路:使用一个变量mindiff来监测和与target之间的差值,如果差值为0,直接返回sum值. 代码: class Solution: # @return an integer def threeSumClosest(self, num, target): num.sort()

【Leetcode】3Sum Closest

题目链接:https://leetcode.com/problems/3sum-closest/ 题目: 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 exac

[LeetCode]30. KSum问题总结

[LeetCode]1. 2Sum题目:https://leetcode.com/problems/two-sum/,解答:http://www.cnblogs.com/aprilcheny/p/4823576.html: [LeetCode]2. 3Sum题目:https://leetcode.com/problems/3sum/,解答:http://www.cnblogs.com/aprilcheny/p/4872283.html: [LeetCode]3. 3Sum Closest题目:h

[Leetcode][016] 3Sum Closest (Java)

题目: https://leetcode.com/problems/3sum-closest/ [标签]Array; Two Pointers [个人分析] 这道题和它的姊妹题 3Sum 非常类似, 就不再多说了,具体一些的分析可以参考 [Leetcode][015] 3Sum 1 public class Solution { 2 public int threeSumClosest(int[] nums, int target) { 3 int result = target; 4 int