• leetcode — 4sum


    import java.util.Arrays;
    import java.util.HashSet;
    import java.util.Set;
    
    /**
     * Source : https://oj.leetcode.com/problems/4sum/
     *
     * Created by lverpeng on 2017/7/10.
     *
     * Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
     * Find all unique quadruplets in the array which gives the sum of target.
     *
     * Note:
     *
     * Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
     * The solution set must not contain duplicate quadruplets.
     *
     *     For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
     *
     *     A solution set is:
     *     (-1,  0, 0, 1)
     *     (-2, -1, 1, 2)
     *     (-2,  0, 0, 2)
     */
    public class FourSum {
    
        /**
         *
         * 分解为多个3sum问题,求出3sum问题普通解法
         * 3sum的解法就是分为多个2sum问题,2sum问题就可以使用原来的方法进行解答
         */
        public Set<Integer[]>  threeSum (int[] num, int total) {
            Set<Integer[]> set = new HashSet<Integer[]>();
    //        Arrays.sort(num);
            for (int i= 0; i < num.length; i++) {
                int left = i + 1;
                int right = num.length - 1;
                int sum2 = total - num[i];
                while (left < right) {
                    if (num[left] + num[right] == sum2) {
                        Integer[] answer = {num[i], num[left], num[right]};
                        set.add(answer);
                        left++;
                        right--;
                        while (left < right && num[left] + num[right] == sum2) {
                            Integer[] ans = {num[i], num[left], num[right]};
                            set.add(ans);
                            left++;
                            right--;
                        }
    
                    } else if (num[left] + num[right] < sum2) {
                        left++;
                        while (left < right && num[left] + num[right] < sum2) {
                            left++;
                        }
                    } else {
                        right--;
                        while (left < right && num[left] + num[right] > sum2) {
                            right--;
                        }
                    }
                }
            }
            return set;
        }
    
    
        /**
         * 对数组排序,分解为3sum处理
         *
         * @param num
         * @param total
         * @return
         */
        public Set<Integer[]> fourSum (int[] num, int total) {
            Set<Integer[]> set = new HashSet<Integer[]>();
            Arrays.sort(num);
            int[] subArr = new int[num.length - 1];
            for (int i = 0; i < num.length; i++) {
                int a = num[i];
                System.arraycopy(num, i + 1, subArr, 0, num.length  - i - 1);
                Set<Integer[]> threeSumResult = threeSum(subArr, total - a);
                for (Integer[] intArr : threeSumResult) {
                    Integer[] arr = new Integer[4];
                    arr[0] = a;
                    System.arraycopy(intArr, 0, arr, 1, 3);
                    set.add(arr);
                }
            }
            return set;
        }
    
        public void printList (Set<Integer[]> set) {
            for (Integer[] arr : set) {
                System.out.println(Arrays.toString(arr));
            }
        }
    
        public static void main(String[] args) {
            FourSum fourSum = new FourSum();
            int[] arr = {1, 0, -1, 0, -2, 2};
            Set<Integer[]> set  = fourSum.fourSum(arr, 0);
            fourSum.printList(set);
        }
    
    }
    
  • 相关阅读:
    [题解] [HNOI2014] 世界树
    [luogu 5301][bzoj 5503] [GXOI/GZOI2019] 宝牌一大堆
    [HDU4507]吉哥系列故事——恨7不成妻
    [国家集训队]聪聪可可
    [模板]点分治
    [2018.8.12]模拟赛B组
    JZOJ5804. 【2018.08.12提高A组模拟】简单的序列
    2018.8.10模拟赛
    2018.8.8模拟赛
    [2018.8.6]模拟赛
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7352752.html
Copyright © 2020-2023  润新知