Created
June 3, 2020 10:13
-
-
Save kiemrong08/f7ef31d6f92b4c5ed21703d4e3019a64 to your computer and use it in GitHub Desktop.
#hashmap
This file contains hidden or 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
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