@@ -0,0 +1,1593 @@
+/*%0D%0AGiven an array S of n integers, find three integers in S such that the sum is closest to a given number, target. %0D%0AReturn the sum of the three integers. You may assume that each input would have exactly one solution.%0D%0A%0D%0A    For example, given array S = %7B-1 2 1 -4%7D, and target = 1.%0D%0A%0D%0A    The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).%0D%0A%0D%0A%0D%0A%0D%0AThougt :%0D%0Ause 3 pointers to point current element, next element and the last element. %0D%0AIf the sum is less than target,%0D%0A     it means we have to add a larger element so next element move to the next.%0D%0AIf the sum is greater, %0D%0A     it means we have to add a smaller element so last element move to the second last element.%0D%0AKeep doing this until the end. %0D%0AEach time compare the difference between sum and target, %0D%0Aif it is less than minimum difference so far, then replace result with it, otherwise keep iterating.%0D%0A*/%0D%0A%0D%0Apublic class Solution %7B%0D%0A    public int threeSumClosest(int%5B%5D nums, int target) %7B%0D%0A       int result = nums%5B0%5D + nums%5B1%5D + nums%5Bnums.length - 1%5D;%0D%0A        Arrays.sort(nums);%0D%0A        for (int i = 0; i %3C nums.length - 2; i++) %7B%0D%0A            int start = i + 1, end = nums.length - 1;%0D%0A            while (start %3C end) %7B%0D%0A                int sum = nums%5Bi%5D + nums%5Bstart%5D + nums%5Bend%5D;%0D%0A                if (sum %3E target) %7B%0D%0A                    end--;%0D%0A                %7D else %7B%0D%0A                    start++;%0D%0A                %7D%0D%0A                if (Math.abs(sum - target) %3C Math.abs(result - target)) %7B%0D%0A                    result = sum;%0D%0A                %7D%0D%0A            %7D%0D%0A        %7D%0D%0A        return result;%0D%0A    %7D%0D%0A%7D

add 3Sum.java