Skip to content

Instantly share code, notes, and snippets.

@kiemrong08
Created June 3, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiemrong08/f7ef31d6f92b4c5ed21703d4e3019a64 to your computer and use it in GitHub Desktop.
Save kiemrong08/f7ef31d6f92b4c5ed21703d4e3019a64 to your computer and use it in GitHub Desktop.
#hashmap
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer,Integer> map=new HashMap<>();
int arr[]=new int [2];
for(int i=0;i<nums.length;i++) {
if(map.containsKey(target-nums[i])) {
arr[0]=map.get(target-nums[i]);
arr[1]=i;
return arr;
}else {
map.put(nums[i],i);
}
}
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment