两数之和-高频
/**
* https://leetcode-cn.com/problems/two-sum/
*/
public class _1_两数之和 {
public int[] twoSum(int[] nums, int target) {
if (nums == null) return null;
// 用来存放之前扫描过的元素
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
Integer idx = map.get(target - nums[i]);
if (idx != null) return new int[]{idx, i};
map.put(nums[i], i);
}
return null;
}
}
浙公网安备 33010602011771号