[LeetCode] 3Sum Closest

This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target. Also, some corner cases need to be handled; for example, nums does not have more than 2 elements.

The code is as follows, which is quite self-explanatory.

 1     int threeSumClosest(vector<int>& nums, int target) {
 2         sort(nums.begin(), nums.end());
 3         while (nums.size() <= 2)
 4             return accumulate(nums.begin(), nums.end(), 0);
 5         int ans = nums[0] + nums[1] + nums[2];
 6         for (int i = 0; i < nums.size() - 2; i++) {
 7             int left = i + 1, right = nums.size() - 1;
 8             while (left < right) {
 9                 int temp = nums[i] + nums[left] + nums[right];
10                 if (abs(temp - target) < abs(ans - target))
11                     ans = temp;
12                 if (temp == target) return ans;
13                 if (temp > target) right--;
14                 else left++;
15             }
16         }
17         return ans;
18     }
时间: 2024-12-23 20:04:44

[LeetCode] 3Sum Closest的相关文章

[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 最近三数之和

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

LeetCode——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 array S = {-1 2

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

题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). 1 int threeSumClosest(vector<int>& nums, int target) { 2 int sum=nums[0]+nums[1]+nums[2]; 3 sort(nums.begin(), nums.end()); 4 for(int i=0; i<nums.size()-2; i++) 5 { 6 int p=i+1, q=nums.size()

[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 016 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 array S = {

leetcode 3Sum 3Sum Closest 4Sum

这几个题很典型也是国外一些知名公司经常会问到的题 3Sum: 排序,避免重复,时间复杂度O(n^2) class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { int len=num.size(); sort(num.begin(),num.begin()+len); vector<vector<int> > ret; ret.clear(); i

3Sum Closest leetcode java

题目: 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 = {-

LeetCode[Array]----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