Skip to content

Instantly share code, notes, and snippets.

@leearmee35
Created March 3, 2017 20:28
Show Gist options
  • Save leearmee35/8906e4417e982fc8da9dc782707c8c97 to your computer and use it in GitHub Desktop.
Save leearmee35/8906e4417e982fc8da9dc782707c8c97 to your computer and use it in GitHub Desktop.
1. Two Sum
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for(int i=0;i<nums.length;i++){
System.out.println(i);
if(map.get(target-nums[i])!=null){
res[0] = map.get(target-nums[i]);
res[1] = i;
return res;
}
if(map.get(nums[i])==null){
map.put(nums[i],i);
}
}
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment