leetcode-3Sum Closest-16

输入一组数字,任选三个数字,求这三个数字加起来最接近某一值的和。

枚举三个数字的话ON^3

更高效的方法,利用了求一组数字中两个数字的和的方法,即用两个指针指向首尾往中间移动,前提是数组有序,所以先排序。遍历数组,然后用两个指针指向这个元素后面序列的首尾,如果这三个数字的和到target的距离比之前的ans到target的距离更小,就更新ans,然后移动后面两个指针:如果当前和大于target,后面的指针往前移,如果小于,前面的指针后移。时间ON^2

 1 class Solution {
 2 public:
 3     int threeSumClosest(vector<int>& nums, int target) {
 4         if(nums.size()<3) return 0;
 5         sort(nums.begin(),nums.end());
 6         int ans=nums[0]+nums[1]+nums[2];
 7         for(int i=0;i<nums.size();i++){
 8             int j=i+1,k=nums.size()-1;
 9             while(j<k){
10                 int sum=nums[j]+nums[k]+nums[i];
11                 if(sum==target) return target;
12                 if(sum<target) j++;
13                 else k--;
14                 int tmp=abs(sum-target);
15                 if(tmp<abs(ans-target)) ans=sum;
16             }
17         }
18         return ans;
19     }
20 };
时间: 2025-01-04 09:08:00

leetcode-3Sum Closest-16的相关文章

[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

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

[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 16. 3Sum Closest(指针搜索)

16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have e

leetCode 16.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][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

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 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 = {