class Solution {
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++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
Integer x = map.get(target - nums[i]);
if (null != x && x != i) {
res[0] = i;
res[1] = x;
}
}
return res;
}
}
LC刷题-1
最新推荐文章于 2022-12-07 10:31:05 发布