import java.util.HashMap; //法1:哈希表 class Solution {public int[] twoSum(int[] numbers, int target) {HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();for(int i = 0; i < numbers.length; i++) {if (map.containsKey(target - numbers[i])) {return new int[] {map.get(target - numbers[i]) + 1, i + 1};} else {map.put(numbers[i], i);}}return new int[] {-1, -1};} }
//法2:双指针 class Solution {public int[] twoSum(int[] numbers, int target) {int l = 0;int r = numbers.length-1;// l不能取等于r,因为不能取重复的元素while(l<r){int sum = numbers[l]+numbers[r];if(sum == target){return new int[]{l+1,r+1};}else if(sum>target){r--;}else{l++;}}return new int[]{-1,-1};} }