Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Last active August 9, 2017 02:51
Show Gist options
  • Save cixuuz/df0b63d9204c29991744b4d63ef71590 to your computer and use it in GitHub Desktop.
Save cixuuz/df0b63d9204c29991744b4d63ef71590 to your computer and use it in GitHub Desktop.
[1. Two Sum] #lc0001
public class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
Map<Integer, Integer> visited = new HashMap<>();
for (int i=0; i<nums.length; i++) {
if (visited.containsKey(nums[i])) {
result[0] = visited.get(nums[i]);
result[1] = i;
return result;
}
visited.put(target-nums[i], i);
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment