1. Two Sum
167. Two Sum II - Input array is sorted
其实我没太看出来这两道题到底有什么区别,似乎按照出题人的考虑第一道题用hashmap来做比较好,后面一道题用两点法来做比较好。这里放在一起讲
- hashmap的方法
首先hashmap的方法是比较容易想到的,从一组数组中找到和为target的两个数,只要用target减数组中的每个数,用这个差值作为key,用索引i作为value。然后从0到length-1遍历即可。整个的复杂度为O(n)。很简单的思路,java代码如下:
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0 ; i < nums.length ; i++){
if(map.keySet().contains(nums[i])){
int[] r = new int[2];
r[0] = map.get(nums[i]);
r[1] = i;
return r;
}
int ret = target - nums[i];
map.put(ret,i);
}
return null;
}
}
两点法:
两点法稍微复杂一些,但是有更强的拓展性,可以用于任意找任意k个数的和。
首先,对这个数组排序,
然后,如果k为2,那么指定left点为索引0和right点为索引length-1,这样如果两个数的和小于target则左节点右移,如果和大于target则右节点左移,如果相等则两节点同时移动。遍历就行。复杂度的话,O(nlgn)+O(n) = O(nlgn)。
public class Solution {
public int[] twoSum(int[] num, int target) {
int[] indice = new int[2];
if (num == null || num.length < 2) return indice;
int left = 0, right = num.length - 1;
while (left < right) {
int v = num[left] + num[right];
if (v == target) {
indice[0] = left + 1;
indice[1] = right + 1;
break;
} else if (v > target) {
right --;
} else {
left ++;
}
}
return indice;
}
}
15. 3Sum
18. 4Sum
对应3Sum的情况,其实就是2sum的拓展了,固定1个点然后退化为2Sum的情况,对于Ksum则可以依次退化为k-1Sum。下面直接贴代码了。3Sum是针对3Sum的实现,4Sum是针对KSum的实现。不过就具体实现来说还是有很多需要注意的部分。实现也是写了很长一段时间。
复杂度的话是O(n^(k-1))
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
// List<List<Integer>> ret = new ArrayList<List<Integer>>();
// List<List<Integer>> ret = new ArrayList<>();
// List<List<Integer>> ret = new ArrayList<ArrayList<Integer>>(); wrong
List<List<Integer>> ret = new ArrayList();
if(nums.length < 3) return ret;
Arrays.sort(nums);
int left = 0;
while(left < nums.length - 2){
if(nums[left] > 0) break;
int middle = left + 1;
int right = nums.length - 1;
while(middle < right){
int sum = nums[left] + nums[middle] + nums[right];
if(sum==0) ret.add(Arrays.asList(nums[left],nums[middle],nums[right]));
if(sum<=0) while(nums[middle++] ==nums[middle] && middle < right);
if(sum>=0) while(nums[right--] == nums[right] && middle < right);
}
while(nums[left++]==nums[left] && left < nums.length - 2);
}
return ret;
}
}
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> ret = new ArrayList();
if(nums.length < 4) return ret;
Arrays.sort(nums);
ret = helper(nums,4,target,0);
return ret;
}
private List<List<Integer>> helper(int[] nums, int k,int target,int pos){
List<List<Integer>> ret = new ArrayList();
if (target < nums[pos]*k || target > nums[nums.length - 1]*k){
return ret;
}
if(k > 2){
int i = pos;
while(i <= (nums.length - k)){
List<List<Integer>>tmp = helper(nums,k-1,target-nums[i],i+1);
for(List<Integer> sub : tmp){
List<Integer> node = new LinkedList<Integer>();
node.add(nums[i]);
node.addAll(sub);
ret.add(node);
}
while(nums[i++]==nums[i] && i <= nums.length - k);
}
}else{//when k ==2
int left = pos;
int right = nums.length - 1;
while(left < right){
int sum = nums[left] + nums[right];
if(sum == target){
List<Integer> tmp = Arrays.asList(nums[left],nums[right]);
ret.add(tmp);
while(nums[left++] == nums[left] && left < right);
while(nums[right--] == nums[right] && left < right);
}else if(sum < target){
while(nums[left++] == nums[left] && left < right);
}else{//sum > target
while(nums[right--] == nums[right] && left < right);
}
}
}
return ret;
}
}