Last active
August 9, 2017 02:51
-
-
Save cixuuz/df0b63d9204c29991744b4d63ef71590 to your computer and use it in GitHub Desktop.
[1. Two Sum] #lc0001
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
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