3Sum Closest 解答

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.

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

Solution

和3Sum的思路基本一样。也是固定起点后,双指针遍历。

 1 public class Solution {
 2     public int threeSumClosest(int[] nums, int target) {
 3         if (nums == null || nums.length < 3) {
 4             return 0;
 5         }
 6         int length = nums.length;
 7         Arrays.sort(nums);
 8         int min = Integer.MAX_VALUE;
 9         int result = nums[0] + nums[1] + nums[2];
10         for (int i = 0; i < length - 2; i++) {
11             // Avoid duplicates
12             if (i > 0 && nums[i] == nums[i - 1]) {
13                 continue;
14             }
15             int l = i + 1, r = length - 1;
16             while (l < r) {
17                 int sum = nums[i] + nums[l] + nums[r];
18                 int sub = Math.abs(sum - target);
19                 if (sub == 0) {
20                     return sum;
21                 } else if (min > sub) {
22                     min = sub;
23                     result = sum;
24                 }
25                 if (sum > target) {
26                     r--;
27                     // Avoid duplicates
28                     while (l < r && nums[r] == nums[r + 1]) {
29                         r--;
30                     }
31                 } else if (sum < target) {
32                     l++;
33                     // Avoid duplicates
34                     while (l < r && nums[l] == nums[l - 1]) {
35                         l++;
36                     }
37                 }
38             }
39         }
40         return result;
41     }
42 }
时间: 2024-11-07 23:07:02

3Sum Closest 解答的相关文章

No.016 3Sum Closest

16. 3Sum Closest Total Accepted: 86565 Total Submissions: 291260 Difficulty: Medium 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

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

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

2Sum,3Sum,4Sum,kSum,3Sum Closest系列

1).2sum 1.题意:找出数组中和为target的所有数对 2.思路:排序数组,然后用两个指针i.j,一前一后,计算两个指针所指内容的和与target的关系,如果小于target,i右移,如果大于,j左移,否则为其中一个解 3.时间复杂度:O(nlgn)+O(n) 4.空间:O(1) 5.代码: void twoSum(vector<int>& nums,int numsSize,int target,vector<vector<int>>& two

leetcode_16题——3Sum Closest(两个指针)

3Sum Closest Total Accepted: 38536 Total Submissions: 143223My Submissions Question Solution 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 ma

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