@@ -0,0 +1,1671 @@
+// 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.%0A%0A//     For example, given array S = %7B-1 2 1 -4%7D, and target = 1.%0A%0A//     The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).%0A%0Apublic class Solution %7B%0A%09// intuitively, brute force approach may solve this problem. O(n3)%0A    // public int threeSumClosest(int%5B%5D num, int target) %7B%0A    // %09int error = Integer.MAX_VALUE;%0A    // %09int sum = 0;%0A    // %09for(int i = 0; i %3C num.length; i++)%7B%0A    // %09%09for(int j = i + 1; j %3C num.length; j++)%7B%0A    // %09%09%09for(int k = j + 1; k %3C num.length; k++)%7B%0A    // %09%09%09%09int tempSum = num%5Bi%5D + num%5Bj%5D + num%5Bk%5D;%0A    // %09%09%09%09if(tempSum == target) return target;%0A    // %09%09%09%09int tempError = Math.abs(target - tempSum);%0A    // %09%09%09%09if(tempError %3C error)%7B%0A    // %09%09%09%09%09error = tempError;%0A    // %09%09%09%09%09sum = tempSum;%0A    // %09%09%09%09%7D%0A    // %09%09%09%7D%0A    // %09%09%7D%0A    // %09%7D%0A%0A    // %09return sum;%0A    // %7D%0A%0A%09// use two pointers technique, reduce O(n3) to O(n2)%0A    public int threeSumClosest(int%5B%5D num, int target) %7B%0A    %09int error = Integer.MAX_VALUE;%0A    %09int sum = 0;%0A%0A    %09Arrays.sort(num);%0A%0A    %09for(int i = 0; i %3C num.length; i++)%7B%0A    %09%09int j = i + 1;%0A    %09%09int k = num.length - 1;%0A%0A    %09%09while(j %3C k)%7B%0A    %09%09%09int tempSum = num%5Bi%5D + num%5Bj%5D + num%5Bk%5D;%0A    %09%09%09if(tempSum == target) return target;%0A    %09%09%09if(Math.abs(target - tempSum) %3C error)%7B%0A    %09%09%09%09error = Math.abs(target - tempSum);%0A    %09%09%09%09sum = tempSum;%0A    %09%09%09%7D%0A%0A    %09%09%09if(tempSum %3C target)%7B%0A    %09%09%09%09j++;%0A    %09%09%09%7Delse%7B%0A    %09%09%09%09k--;%0A    %09%09%09%7D%0A    %09%09%7D%0A    %09%7D%0A%0A    %09return sum;%0A    %7D%0A%7D

add 3sum colsest solution