zoukankan      html  css  js  c++  java
  • Lc18-四数之和

    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Given an array nums of n integers and an integer target, are there elements
     * a, b, c, and d in nums such that a + b + c + d = target? Find all unique
     * quadruplets in the array which gives the sum of target.
     * 
     * Note:
     * 
     * The solution set must not contain duplicate quadruplets.
     * 
     * Example:
     * 
     * Given array nums = [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] ]
     * 
     * 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/4sum
     * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
     *
     */
    public class Lc18 {
    
        public static List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> res = new ArrayList<>();
            if (nums.length < 4) {
                return res;
            }
            // 排序
            shellSort(nums);
    
            for (int i = 0; i < nums.length - 3; i++) {
                // 减少循环
    //            if (nums[i] > target) {
    //                break;
    //            }
                for (int j = i + 1; j < nums.length - 2; j++) {
                    for (int left = j + 1, right = nums.length - 1; left < nums.length && right >= 0 && left < right;) {
                        // 减少循环
                        if (nums[i] + nums[j] + nums[left] + nums[right] > target) {
                            right--;
                        } else if (nums[i] + nums[j] + nums[left] + nums[right] < target) {
                            left++;
                        } else {
                            List<Integer> tempResList = new ArrayList<>();
                            tempResList.add(nums[i]);
                            tempResList.add(nums[j]);
                            tempResList.add(nums[left]);
                            tempResList.add(nums[right]);
                            left++;
                            if (!res.contains(tempResList)) {
                                res.add(tempResList);
                            }
                        }
                    }
                }
    
            }
            return res;
    
        }
    
        // 希尔排序
        private static void shellSort(int[] nums) {
            for (int gap = nums.length / 2; gap > 0; gap /= 2) {
                for (int i = gap; i < nums.length; i++) {
                    for (int j = i; j >= gap && nums[j - gap] > nums[j]; j -= gap) {
                        int temp = nums[j];
                        nums[j] = nums[j - gap];
                        nums[j - gap] = temp;
                    }
                }
            }
        }
    
        public static void main(String[] args) {
            int nums[] = {1,-2,-5,-4,-3,3,3,5};
            int target = -11;
            List<List<Integer>> res = fourSum(nums, target);
            System.out.println(res);
        }
    
    }
  • 相关阅读:
    转:线程Thread (1)
    jquery 比较全面的API中文版地址
    IE 出现stack overflow 报错的原因归纳
    转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)
    转:双向链表dblinklist
    转:stack
    转:queue
    解决获取同胞元素空白节点影响的问题
    取10到100的随机数方法
    进度条
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/12182666.html
Copyright © 2011-2022 走看看