class Solution {
public int[] twoSum(int[] nums, int target) {
int[] result = new int[2];
if (nums == null || nums.length == 0) {
return result;
}
HashMap<Integer, Integer> valueIndexMap = new HashMap<>();
for (int i=0; i<nums.length; i++) {
int rest = target - nums[i];
Integer leftIndex = valueIndexMap.get(rest);
if (leftIndex != null) {
result[0] = leftIndex;
result[1] = i;
return result;
}
valueIndexMap.put(nums[i], i);
}
return result;
}
}
[leetcode][easy]Two Sum
最新推荐文章于 2022-01-13 18:32:53 发布