1、力扣原题
- 思路:哈希表
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
int count = nums.length;
int[] res = new int[2];
for (int i = 0; i < count; i++) {
int key = nums[i];
int value = i;
int otherValue = target - key;
if (map.containsKey(otherValue)) {
res[0] = map.get(otherValue);
res[1] = i;
break;
}
map.put(key,value);
}
return res;
}
}