@@ -0,0 +1,1350 @@
+import java.util.Arrays;%0A%0A/**%0A * Given an integer array, find a pair from it that their sum is closest to 0.%0A * %3Cp%3E%0A * Created by kiyan on 6/3/16.%0A */%0Apublic class TwoSumClosestPair %7B%0A%0A    public int%5B%5D closest(int%5B%5D nums) %7B%0A        int closest = Integer.MAX_VALUE;%0A        int%5B%5D res = new int%5B2%5D;%0A        for (int i = 0; i %3C nums.length - 1; i++) %7B%0A            for (int j = i + 1; j %3C nums.length; j++) %7B%0A                int sum = nums%5Bi%5D + nums%5Bj%5D;%0A                if (Math.abs(sum) %3C closest) %7B%0A                    closest = sum;%0A                    res%5B0%5D = nums%5Bi%5D;%0A                    res%5B1%5D = nums%5Bj%5D;%0A                %7D%0A            %7D%0A        %7D%0A        return res;%0A    %7D%0A%0A    public int%5B%5D closestB(int%5B%5D nums) %7B%0A        if (nums == null %7C%7C nums.length %3C= 1) return null;%0A        if (nums.length == 2) return new int%5B%5D%7Bnums%5B0%5D, nums%5B1%5D%7D;%0A%0A        Arrays.sort(nums);%0A        int%5B%5D res = new int%5B2%5D;%0A        int i = 0;%0A        int j = nums.length - 1;%0A        int closest = Integer.MAX_VALUE;%0A        while (i %3C j) %7B%0A            int sum = nums%5Bi%5D + nums%5Bj%5D;%0A%0A            if (Math.abs(sum) %3C closest) %7B%0A                closest = sum;%0A                res%5B0%5D = nums%5Bi%5D;%0A                res%5B1%5D = nums%5Bj%5D;%0A            %7D%0A%0A            if (sum %3E 0) j--;%0A            else if (sum %3C 0) i++;%0A            else return res;%0A        %7D%0A        return res;%0A    %7D%0A%7D%0A

add two sum closest