/**
 * Problem: #1 Two Sum
 * Author: xiao0720
 * Idea: iterate through each number in the nums, compare the difference between target
 * and the current number, find if the difference exists as an item in the rest of the
 * array nums.
 */

public class TwoSum {
    public static int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j< nums.length; j++) {
                if (target - nums[i] == nums[j]) {
                    return new int[] {i, j};
                }
            }
        }
        throw new IllegalArgumentException("Now solution found.");
    }

    public static void main(String[] args) {
        int[] nums = new int[] {2, 7, 11, 15};
        int target = 9;
        int[] result = twoSum(nums, target);

        System.out.println("[" + result[0] + ", " + result[1] + "]");
  }
}

@@ -1,8 +1,63 @@
+import java.util.HashMap;%0Aimport java.util.Hashtable;%0A%0A
 /**%0A * P
@@ -81,25 +81,8 @@
 m%0A *
- Author: xiao0720
 %0A * 
@@ -268,16 +268,174 @@
 y nums.%0A
+ *%0A * Solutions:%0A *  twoSum: Brute Force O(n%5E2)%0A *  twoSumHash2: Use hash table to trade space for time O(n)%0A *  twoSumHash: One loop, examining and add new.%0A
  */%0A%0Apub
@@ -454,29 +454,30 @@
 oSum %7B%0A    p
-ublic
+rivate
  static int%5B
@@ -826,16 +826,1021 @@
 %0A    %7D%0A%0A
+    private static int%5B%5D twoSumHash2(int%5B%5D nums, int target) %7B%0A        Hashtable%3CInteger, Integer%3E hash = new Hashtable%3C%3E();%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            hash.put(nums%5Bi%5D, i);%0A        %7D%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            int diff = target - nums%5Bi%5D;%0A            if (hash.containsKey(diff) && hash.get(diff) != i) %7B%0A                return new int%5B%5D %7Bi, hash.get(diff)%7D;%0A            %7D%0A        %7D%0A        throw new IllegalArgumentException(%22No solution found%22);%0A    %7D%0A%0A    private static int%5B%5D twoSumHash(int%5B%5D nums, int target) %7B%0A        HashMap%3CInteger, Integer%3E hash = new HashMap%3C%3E();%0A%0A        for (int i = 0; i %3C nums.length; i++) %7B%0A            int complement = target - nums%5Bi%5D;%0A            if (hash.containsKey(complement) && hash.get(complement) != i) %7B%0A                return new int%5B%5D %7Bhash.get(complement), i%7D;%0A            %7D%0A            hash.put(nums%5Bi%5D, i);%0A        %7D%0A        throw new IllegalArgumentException(%22No solutions found%22);%0A    %7D%0A%0A
     publ
@@ -1947,16 +1947,17 @@
 et = 9;%0A
+%0A
         
@@ -1968,16 +1968,17 @@
 %5D result
+0
  = twoSu
@@ -1993,16 +1993,117 @@
 target);
+%0A        int%5B%5D result1 = twoSumHash2(nums, target);%0A        int%5B%5D result2 = twoSumHash(nums, target);
 %0A%0A      
@@ -2135,16 +2135,17 @@
 + result
+0
 %5B0%5D + %22,
@@ -2155,16 +2155,161 @@
 + result
+0%5B1%5D + %22%5D%22);%0A        System.out.println(%22%5B%22 + result1%5B0%5D + %22, %22 + result1%5B1%5D + %22%5D%22);%0A        System.out.println(%22%5B%22 + result2%5B0%5D + %22, %22 + result2
 %5B1%5D + %22%5D

Add new solutions to the problem