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

 思路:

  本题的解法和上一次3Sum的解法有点类似,我们可以先将数组排序,然后将数组中的数从左到右依次确定为第一个数,在其右边的数中寻找最接近的target的数即可。重要的是判断逻辑的思路。代码如下:

 1 public int threeSumClosest(int[] nums, int target) {
 2     if (nums == null || nums.length < 3) {
 3         return 0;
 4     }
 5
 6     Arrays.sort(nums);
 7
 8     int closest = nums[0]+nums[1]+nums[2] ;
 9
10     for(int i = 0 ; i < nums.length-2 ; i++){
11         int l = i+1 ;
12         int r = nums.length-1 ;
13         while(l < r){
14             int sum = nums[i] + nums[l] + nums[r] ;
15             if(sum == target){
16                 return sum ;
17             }else if(sum < target){
18                 /*
19                  * 三种情况:
20                  *     1、closest < sum < target --->closest = sum
21                  *     2、sum < target < closest --->| target-sum < closest-target ---> closest = sum
22                  *                                   | target-sum >= closest-target --> closest不变
23                  *     3、sum <= closest <= target ---> closest不变
24                  */
25                 if(sum > closest){
26                     //情况1
27                     closest = sum ;
28                 }else if(closest > target){
29                     //情况2
30                     if(target-sum < closest-target){
31                         //情况2.1,
32                         closest = sum ;
33                     }
34                     //情况2.2不用变化
35                 }
36                 //情况3不同变化
37                 l++ ;
38             }else{
39                 //分析同上
40                 if(sum < closest){
41                     closest = sum ;
42                 }else if(closest < target){
43                     if(sum-target <= target-closest){
44                         closest = sum ;
45                     }
46                 }
47                 r-- ;
48             }
49         }
50
51     }
52     return closest ;
53 }
时间: 2024-10-27 05:34:58

No.016 3Sum Closest的相关文章

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] 016. 3Sum Closest (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 016.3Sum_Closest (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum-closest/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和最接近 target. 分析:

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

Java for 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 = {-1 2

016 3SUM Closest

这道题与 015 3 SUM 基本思路一样 都是夹逼思想  复杂度 O(n*n) class Solution: # @param {integer[]} nums # @param {integer} target # @return {integer} def threeSumClosest(self, nums, target): length = len(nums) nums = sorted(nums) ans = sum(nums[0:3]) i = 0 while i < leng

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