LeetCode Notes 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 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

个人觉得这个是比较简单的一道题哈,我比较擅长这种。唯一比较难的就是最后代码的那个while loop的运用。

对我来说如果没有想到这个while的话可能就会复杂很多了。主要是不要一直觉得要从头开始3个数字或者从后面开始3个数字这样比较固化的思维。

而且对于这种杂乱的数列,先sorting整理是很有必要的。

同样的就是设定初始比较值那个min的时候,想不到怎么设索性就来个最大的不就对了嘛。

public class Solution {
    public int threeSumClosest(int[] nums, int target) {
       if(nums==null||nums.length==0){
           return -1;
       }
       int result=0;
       int min=Integer.MAX_VALUE;
       Arrays.sort(nums);
       for(int i=0;i<nums.length;i++){
           int j=i+1;
           int k=nums.length-1;
           while(j<k){
               int sum=nums[i]+nums[j]+nums[k];
               int diff=Math.abs(sum-target);
               if(diff==0){
                   return sum;
               }
               if(diff<min){
                   min=diff;
                   result=sum;
               }
               if(sum<=target){
                   j++;
               }else{
                   k--;
               }
           }
       }
       return result;

    }
}
时间: 2024-07-29 11:22:27

LeetCode Notes 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][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 (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

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

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 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 (M)

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

Java [leetcode 16] 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 16 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