leetcode hot 100 刷题记录

leetcode hot 100 刷题记录

1. 两数之和

1
2
3
4
5
6
7
8
9
10
11
12
// https://leetcode.cn/problems/two-sum
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i ++) {
int tmp = target - nums[i];
if (map.containsKey(tmp)) {
return new int[]{i, map.get(tmp)};
}
map.put(nums[i], i);
}
return new int[]{-1, -1};
}