Subarray Sum Closest

1. 保存<sum[i + 1], i>,然后对sum[i + 1]排序,然后找出相邻值之间的最小差。

2. TreeMap:ceilingEntry返回一个键-值映射关系,它与大于等于给定键的最小键关联Value。floorEntry:返回小于等于给定键的最大键关联Value。

    public ArrayList<Integer> subarraySumClosest(int[] nums) {
        // write your code here
        ArrayList<Integer> res = new ArrayList<Integer>();
        if (nums == null || nums.length == 0) {
            return res;
        }
        TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
        int sum = 0;
        int minDif = Integer.MAX_VALUE;
        res.add(0);
        res.add(0);
        for (int i = 0; i < nums.length; ++i) {
            sum += nums[i];
            Map.Entry floor = map.floorEntry(sum);
            Map.Entry ceiling = map.ceilingEntry(sum);
            if (floor != null || ceiling != null) {
                if (floor == null || (ceiling != null && Math.abs((int) floor.getKey() - sum) > Math.abs((int) ceiling.getKey() - sum))) {
                    if (Math.abs((int) ceiling.getKey() - sum) < minDif) {
                        res.set(0, (int) ceiling.getValue() + 1);
                        res.set(1, i);
                        minDif = Math.abs((int) ceiling.getKey() - sum);
                    }
                } else {
                    if (Math.abs((int) floor.getKey() - sum) < minDif) {
                        res.set(0, (int) floor.getValue() + 1);
                        res.set(1, i);
                        minDif = Math.abs((int) floor.getKey() - sum);
                    }
                }
            }
            map.put(sum, i);
        }
        return res;
    }

4 Sum

难题是如何去重。3sum里面的判断nums[i] == nums[i - 1],已经行不通了。

    public ArrayList<ArrayList<Integer>> fourSum(int[] nums, int target) {     
        //write your code here
        ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
        Arrays.sort(nums);
        ArrayList<String> mp = new ArrayList<>();
        for (int i = 0; i < nums.length; ++i) {
            for (int j = i + 1; j < nums.length; ++j) {
                int p1 = j + 1, p2 = nums.length - 1;
                while (p1 < p2) {
                    int d = nums[i] + nums[j] + nums[p1] + nums[p2] - target;
                    if (d < 0) {
                        ++p1;
                    } else if (d > 0) {
                        --p2;
                    } else {
                        String tag = nums[i] + "#" + nums[j] + "#" + nums[p1] + "#" + nums[p2];
                        if (!mp.contains(tag)) {
                            ArrayList<Integer> ar = new ArrayList<>();
                            ar.add(nums[i]);
                            ar.add(nums[j]);
                            ar.add(nums[p1]);
                            ar.add(nums[p2]);
                            ret.add(ar);
                            mp.add(tag);
                        }
                        ++p1;
                        --p2;
                    }
                }
            }
        }
        return ret;
    }