public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
int idx = map.get(nums[i]);
res[0] = idx;
res[1] = i;
return res;
} else {
map.put(target - nums[i], i);
}
}
return res;
}
Two Sum
最新推荐文章于 2022-08-01 13:16:55 发布