Lc18-四数之和

Posted xiaoshahai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
    }

}

以上是关于Lc18-四数之和的主要内容,如果未能解决你的问题,请参考以下文章

代码随想录算法训练营第7天 | ● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 ● 18. 四数之和 ● 总结

代码随想录算法训练营第七天 | 454.四数相加II ,383. 赎金信 ,15. 三数之和,18. 四数之和

LeetCode 18. 四数之和

leetcode-----18. 四数之和

LeetCode 18. 四数之和

leetcode 每日一题 18. 四数之和