Skip to content

Latest commit

 

History

History
13 lines (13 loc) · 386 Bytes

001 Two Sum.mdown

File metadata and controls

13 lines (13 loc) · 386 Bytes
public int[] twoSum(int[] sources, int target){
  Map<Integer, Integer> storedMap = new HashMap<Integer, Integer>();
  for(int i = 0; i < sources.length; ++i){
    if(storedMap.containsKey(target - sources[i])){
      //下标从1开始
      return new int{storedMap.get(target - sources[i]) + 1, i + 1};
    }
    storedMap.put(sources[i], i);
  }
  return null;
}