LeetCode-第十六题:3Sum Closest

题目

LeetCode-第十六题:3Sum Closest_第1张图片
题目

源码

public class Solution 
{
    public int threeSumClosest(int[] nums, int target) 
    {
        Arrays.sort(nums);
        int result=nums[0]+nums[1]+nums[2];
        int dis=result-target;
        int len=nums.length;
        for(int i=0;i0 && nums[i]==nums[i-1])
            {
                continue;
            }
            int j=i+1;int m=len-1;
            int goal=target-nums[i];
            while(jMath.abs(nums[j]+nums[m]-goal))
                {
                    dis=nums[j]+nums[m]-goal;
                    result=nums[i]+nums[j]+nums[m];
                }
                if(nums[j]+nums[m]==goal)
                {
                    return target;
                }
                else if(nums[j]+nums[m]

你可能感兴趣的:(LeetCode-第十六题:3Sum Closest)