/*
这是用 HashMap 做的,需要耗费额外空间。适用的情况是 Array 没有被 sort
Time : O(n)
Space: O(n)
*/
public class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return new int[] {-1, -1};
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.get(nums[i]) != null) {
int[] result = new int[] {map.get(nums[i]), i};
return result;
}
map.put(target - nums[i], i);
}
return new int[] {-1, -1};
}
}
/*
这种方法使用于 Array 已经被 sort 好了。可以直接用双指针扫。
Time: O(n)
Space: O(1)
*/
public class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return new int[] {-1, -1};
}
int start = 0, end = nums.length - 1;
while (start < end) {
int sum = nums[start] + nums[end];
if (sum == target) {
return new int[] {start, end};
} else if (sum < target) {
start++;
} else {
end--;
}
}
return new int[] {-1, -1};
}
}