class Solution {
public int[] twoSum(int[] numbers, int target) {
int len = numbers.length;
HashMap<Integer, Integer> map = new HashMap<>();
for(int i=0; i<len; i++){
int next = target - numbers[i];
if(map.containsKey(next)){
return new int[]{map.get(next)+1, i+1};
}
map.put(numbers[i], i);
}
return new int[]{-1,-1};
}
}
class Solution {
public int[] twoSum(int[] numbers, int target) {
int len = numbers.length;
int low = 0, high = len-1;
while(low <= high){
int sum = numbers[low] + numbers[high];
if(sum == target)
return new int[]{low+1, high+1};
if(sum < target)
low++;
else high--;
}
return new int[]{-1, -1};
}
}
167. 两数之和 II - 输入有序数组 + 哈希表 + 双指针
原文:https://www.cnblogs.com/GarrettWale/p/14465061.html