4sum, 4sum closest

4sum

[抄题]:

[思维问题]:

  1. 以为很复杂,其实是“排序+双指针”的最高阶模板

[一句话思路]:

[输入量特别大怎么办]:

[画图]:

[一刷]:

  1. 先排序
  2. if (i > 0 && nums[i] == nums[i - 1]) ,  之前的已经操作过
    continue;退出

  3. ArrayList<Integer> temp = new ArrayList<Integer>();//临时数组在哪里用就在哪里临时定义
  4. 等于target时要继续left++ right--
  5. corner case 一定是返回rst自身[],不是null字母 

[总结]:

先排序,然后一吃通吃

[复杂度]:

[英文数据结构,为什么不用别的数据结构]:

  1. res数组用linkedlist arraylist都可以

[其他解法]:

[Follow Up]:

[题目变变变]:

 

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> rst = new LinkedList<List<Integer>>();
        
        if (nums == null || nums.length < 4) {
            return rst;
        }
        
        Arrays.sort(nums);
        
        for(int i = 0; i < nums.length - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) 
                continue;
            
            for(int j = i + 1; j < nums.length - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) 
                continue;
                
                int left = j + 1;
                int right = nums.length - 1;
                while(left < right) {
                if (nums[i] + nums[j] + nums[left] + nums[right] <target) {
                    left++;
                }
                else if (nums[i] + nums[j] + nums[left] + nums[right] >target) {
                    right--;
                }
                else {
                    while (left < right && nums[left] == nums[left + 1]) {
                        left++;
                    }
                    while (left < right && nums[right] == nums[right - 1]) {
                        right--;
                    }
                    
                    List<Integer> temp = new LinkedList<Integer>();
                    temp.add(nums[i]);
                    temp.add(nums[j]);
                    temp.add(nums[left]);
                    temp.add(nums[right]);
                    rst.add(temp);
                    left++;
                    right--;
                }
                }
            }
        }
        return rst;
    }
}
View Code

 

4sum 数组

[抄题]:

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.

To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.

Input:
A = [ 1, 2]
B = [-2,-1]
C = [-1, 2]
D = [ 0, 2]

Output:
2

Explanation:
The two tuples are:
1. (0, 0, 0, 1) -> A[0] + B[0] + C[0] + D[1] = 1 + (-2) + (-1) + 2 = 0
2. (1, 1, 0, 0) -> A[1] + B[1] + C[0] + D[0] = 2 + (-1) + (-1) + 0 = 0

[思维问题]:

  1. 不能利用key的唯一性:把两个元素加在一起作为一个整体即可
  2. map.getOrDefault(sum, 0)可以查找或初始定义出value的值

[一句话思路]:

用hashmap,把两个数的和作为一组,先存后查

[输入量特别大怎么办]:

[画图]:

[一刷]:

  1. 用get函数查的,老师说空指针错误

[总结]:

没有某个sum做index的时候很尴尬。此题两次运用了机智的map.getOrDefault(sum,0)函数

[复杂度]:

Time complexity: O(n^2) Space complexity: O(n^2)

新建一个HashMap不需要给定N空间,是根据新的Key来分配空间,Key的数量是可预测的。

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

class Solution {
    public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
        int rst = 0;
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for(int i = 0; i < A.length; i++) {
            for(int j = 0; j < B.length; j++) {
                int sum = A[i] + B[j];
                map.put(sum,map.getOrDefault(sum,0) + 1);
            }
        }
        
        for(int i = 0; i < C.length; i++) {
            for(int j = 0; j < D.length; j++) {
                    rst += map.getOrDefault(-(C[i] + D[j]),0);
            }
        }
        return rst;
    }
}
View Code

 

posted @ 2017-12-17 20:46  苗妙苗  阅读(251)  评论(0编辑  收藏  举报