Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
首先审题:这个数组总有且只有一个解,那么我们可以知道:数组中如果出现重复元素,那么解一定是这两个重复元素,否则会违背题意说的是有一个解,因此在用hashmap的时候我们不需要判断key重复的情况。另外,注意输出是两个index的值。
解法一:HashMap, 时间复杂度是O(n), 空间复杂度为O(1).
用hashmap从数组的左边向右边遍历,每次先判断target-nums[i]是不是在map里出现过,如果出现过,那么nums[i] + (target - nums[i]) == target, 因此我们就找到了另一个值,如果没有出现过,我们就把(nums[i]和i)放进hashmap.
代码:
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> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
result[0] = map.get(target - nums[i]);
result[1] = i;
return result;
} else {
map.put(nums[i],i);
}
}
return result;
}
}
Note:
这道题不能用双指针的解法,因为要返回的是index, 应用双指针的前提是排序,而排序了以后就没有index的信息了,因此这道题不能用two pointer的解法。
Follow up 1:
如果数组中有重复的元素,返回所有unique的数字组合。例如[1, 2, 2, 3, 3, 4, 4, 5], target = 6, 返回:[[1, 5], [2, 4], [3, 3]].
我是用用双指针来解,hashmap的话的没法解,如果用一个set来对结果去重,那么[3, 3]这种答案就没法得到,用hashmap的话我没有想到其他去重的方法,如果你想到了欢迎给我评论留言,
public static ArrayList<ArrayList<Integer>> twoSum(int[] nums, int target) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (nums == null || nums.length == 0) return result;
Arrays.sort(nums);
int left = 0, right = nums.length - 1;
while (left < right) {
if (nums[left] == nums[left + 1] && left + 1 != right) left++;
if (nums[right] == nums[right - 1] && left + 1 != right) right--;
int sum = nums[left] + nums[right];
if (sum == target) {
ArrayList<Integer> cur = new ArrayList<Integer>();
cur.add(nums[left++]);
cur.add(nums[right--]);
result.add(cur);
} else if (sum > target) {
right--;
} else {
left++;
}
}
return result;
}
Follow up 2:
返回所有unique的index组合, 其中index1 < index2, 例如[1, 2, 2, 3, 3, 4, 4, 5], target = 6, 返回:[[0, 7], [1, 5], [1, 6], [2, 5], [2, 6], [3, 4]]. Note: 每次遇到一次sum == target,都对右边进行判断,如果右边有重复就走一遍所有重复的右边,并且把有效的答案加进来。对于左边也做判断,如果左边有重复,那么右边的right就不减一,下一次进入while时对于重复的左边数字来说,右边的又会重新来一遍,所以得到所有的组合。
public static ArrayList<ArrayList<Integer>> twoSum(int[] nums, int target) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if (nums == null || nums.length == 0) return result;
Arrays.sort(nums);
int left = 0, right = nums.length - 1;
while (left < right) {
int sum = nums[left] + nums[right];
if (sum == target) {
ArrayList<Integer> cu = new ArrayList<Integer>();
cu.add(left);
cu.add(right);
result.add(cu);
int temp = right;
while (nums[temp] == nums[temp - 1] && left < temp - 1) {
ArrayList<Integer> c = new ArrayList<Integer>();
c.add(left);
c.add(--temp);
result.add(c);
}
left++;
if (nums[left] != nums[left + 1]) right--;
} else if (sum > target) {
right--;
} else {
left++;
}
}
return result;
}
总结:
hashmap对于解决原问题的时间复杂度比较优秀 O(n),但是需要extra space O(n)。双指针时间复杂度为O(nlogn),不需要额外空间,而且在解决follow up的出现重复元素的结果去重,返回unique index上更加灵活。