import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/two-sum/
 */
public class TwoSum {

    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int a = nums[i];
            if (map.containsKey(target - a)) {
                return new int[]{i, map.get(target - a)};
            }
            map.put(a, i);
        }
        return null;
    }

    public int[] twoSum2(int[] sortedNums, int target) {
        int i = 0, j = sortedNums.length - 1;
        while (i < j) {
            int sum = sortedNums[i] + sortedNums[j];
            if (sum < target) {
                i++;
            } else if (sum > target) {
                j--;
            } else {
                return new int[]{i, j};
            }

        }
        return null;
    }

    public static void main(String[] args) {
        TwoSum twoSum = new TwoSum();
        int[] nums = new int[]{1, 3, 5, 7, 8, 9, 10, 12, 14, 18};
        int target = 20;

        int[] results = twoSum.twoSum(nums, target);
        System.out.println(String.format("%d, %d", results[0], results[1]));

        results = twoSum.twoSum2(nums, target);
        System.out.println(String.format("%d, %d", results[0], results[1]));
    }
}

@@ -378,49 +378,271 @@
     
-return new int%5B%5D%7Bi, map.get(target - a)%7D;
+int index1 = i + 1;%0A                int index2 = map.get(target - a) + 1;%0A                if (index1 %3C index2) %7B%0A                    return new int%5B%5D%7Bindex1, index2%7D;%0A                %7D else %7B%0A                    return new int%5B%5D%7Bindex2, index1%7D;%0A                %7D
 %0A   

update TwoSum