Leetcode1:两数之和
思路:哈希表存储
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
int num1 = nums[i];
int num2 = target - num1;
if (map.containsKey(num2)) {
return new int[]{map.get(num2), i};
}
map.put(num1, i);
}
return new int[0];
}
}