package leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * https://leetcode.com/problems/two-sum/
 */
public class Problem1 {
    public int[] twoSum(int[] numbers, int target) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            if (!map.containsKey(numbers[i])) {
                map.put(numbers[i], new ArrayList<>());
            }
            map.get(numbers[i]).add(i);
        }
        Arrays.sort(numbers);
        for (int i = 0; i < numbers.length; i++) {
            int val = target - numbers[i];
            int j = Arrays.binarySearch(numbers, i, numbers.length, val);
            if (j < 0) {
                continue;
            } else {
                if (numbers[i] == numbers[j]) {
                    int a = map.get(numbers[i]).get(0) + 1;
                    int b = map.get(numbers[i]).get(1) + 1;
                    return (a < b) ? new int[]{a, b} : new int[]{b, a};
                } else {
                    int a = map.get(numbers[i]).get(0) + 1;
                    int b = map.get(numbers[j]).get(0) + 1;
                    return (a < b) ? new int[]{a, b} : new int[]{b, a};
                }
            }
        }
        return new int[]{};
    }
}

@@ -29,25 +29,23 @@
 va.util.
-ArrayList
+HashMap
 ;%0Aimport
@@ -55,22 +55,23 @@
 va.util.
-Arrays
+HashSet
 ;%0Aimport
@@ -73,36 +73,32 @@
 mport java.util.
-Hash
 Map;%0Aimport java
@@ -107,34 +107,11 @@
 til.
-List;%0Aimport java.util.Map
+Set
 ;%0A%0A/
@@ -216,19 +216,16 @@
 nt%5B%5D num
-ber
 s, int t
@@ -254,19 +254,18 @@
 nteger, 
-Lis
+Se
 t%3CIntege
@@ -314,35 +314,32 @@
 t i = 0; i %3C num
-ber
 s.length; i++) %7B
@@ -371,27 +371,24 @@
 tainsKey(num
-ber
 s%5Bi%5D)) %7B%0A   
@@ -407,27 +407,24 @@
  map.put(num
-ber
 s%5Bi%5D, new Ar
@@ -425,16 +425,14 @@
 new 
-ArrayLis
+HashSe
 t%3C%3E(
@@ -468,27 +468,24 @@
  map.get(num
-ber
 s%5Bi%5D).add(i)
@@ -500,38 +500,8 @@
   %7D%0A
-        Arrays.sort(numbers);%0A
     
@@ -523,27 +523,24 @@
 = 0; i %3C num
-ber
 s.length; i+
@@ -560,19 +560,17 @@
     int 
-val
+a
  = targe
@@ -576,19 +576,16 @@
 et - num
-ber
 s%5Bi%5D;%0A  
@@ -599,90 +599,30 @@
    i
-nt j = Arrays.binarySearch(numbers, i, numbers.length, val);%0A            if (j %3C 0
+f (!map.containsKey(a)
 ) %7B%0A
@@ -652,39 +652,32 @@
 e;%0A            %7D
- else %7B
 %0A               
@@ -677,238 +677,124 @@
     
-    if (numbers%5Bi%5D == numbers%5Bj%5D) %7B%0A                    int a = map.get(numbers%5Bi%5D).get(0) + 1;%0A                    int b = map.get(numbers%5Bi%5D).get(1) + 1;%0A                    return (a %3C b) ? new int%5B%5D%7Ba, b%7D : new int%5B%5D%7Bb, a%7D;%0A  
+Set%3CInteger%3E indexes = map.get(a);%0A            if (a == nums%5Bi%5D && indexes.size() == 1) %7B%0A                continue;%0A
     
@@ -805,18 +805,9 @@
     
-  %7D else %7B
+%7D
 %0A   
@@ -819,118 +819,27 @@
     
-        int a = map.get(numbers%5Bi%5D).get(0) + 1;%0A                    int b = map.get(numbers%5Bj%5D).get(0) + 1;%0A  
+indexes.remove(i);%0A
     
@@ -850,30 +850,14 @@
     
-      
 return
- (a %3C b) ?
  new
@@ -867,64 +867,38 @@
 t%5B%5D%7B
-a, b%7D : new int%5B%5D%7Bb, a%7D;%0A                %7D%0A            %7D
+i, indexes.iterator().next()%7D;
 %0A   

Update problem 1