Skip to content

Instantly share code, notes, and snippets.

@ikaruce
Last active May 29, 2021 16:18
Show Gist options
  • Save ikaruce/17caac78246ccd79f6719e0d4731a939 to your computer and use it in GitHub Desktop.
Save ikaruce/17caac78246ccd79f6719e0d4731a939 to your computer and use it in GitHub Desktop.
LeetCode 1. two sum
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> h = new HashMap<Integer, Integer>();
int i = 0;
for(; i< nums.length; i++){
if(h.get(nums[i]) == null){
h.put(target-nums[i], i);
}else{
break;
}
}
return new int[] {i, h.get(nums[i])};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment