Created
March 3, 2017 20:28
-
-
Save leearmee35/8906e4417e982fc8da9dc782707c8c97 to your computer and use it in GitHub Desktop.
1. Two Sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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