美文网首页
四数之和

四数之和

作者: 小白学编程 | 来源:发表于2018-10-28 11:27 被阅读0次

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

答案中不可以包含重复的四元组。

示例:

给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]

思路

最简单直接的暴力,不出意外地超时

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> L = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        
        
        for (int i = 0; i < nums.length; i++) {
            
            for (int j = i + 1; j < nums.length; j++) {
                for (int k = j + 1; k < nums.length; k++) {
                    for (int l = k + 1; l < nums.length; l++) {
                        List<Integer> list = new ArrayList<Integer>();
                        if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
                            list.add(nums[i]);
                            list.add(nums[j]);
                            list.add(nums[k]);
                            list.add(nums[l]);
                        }
                        int f = 1;
                        for (int a = 0; a < L.size(); a++) {
                            if(L.get(a).equals(list)) {
                                f = 0;
                            }
                        }
                        if (f == 1) {
                            if (list.size() != 0) {
                                 L.add(list);
                            }
                           
                        }
                        
                    }
                }
            }
        }
        return L;
        
    }
}

思路

先遍历两个数,剩下的两个数采用双指针的方式扫描

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> L = new ArrayList<List<Integer>>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                int l = j + 1;
                int r = nums.length - 1;
                int temp = target - nums[i] - nums[j];
                while (l < r) {
                    if (nums[l] + nums[r] == temp) {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[l]);
                        list.add(nums[r]);
                        int f = 1;
                        for (int a = 0; a < L.size(); a++) {
                            if(L.get(a).equals(list)) {
                                f = 0;
                                break;
                            }
                        }
                        if (f == 1) {
                             L.add(list);
                        }
                        r--;
                        l++;
                    }else if (nums[l] + nums[r] > temp) {
                        r--;
                    }else if (nums[l] + nums[r] < temp) {
                        l++;
                    }
                    
                }
            }
        }
        return L;
        
    }
}

相关文章

  • algrithrom

    求和问题,双指针解决 done 两数之和 三数之和 最接近三数之和 四数之和 链表反转问题 done 链表反转 链...

  • 两数之和&三数之和&四数之和&K数之和

    今天看了一道谷歌K数之和的算法题,忽然想起来之前在力扣上做过2、3、4数之和的题,觉得很有必要来整理一下。其实2、...

  • LeetCode 第18题:四数之和

    1、前言 2、思路 采用三数之和的思路,原本三数之和可以分解为:数组中的一个数 + 此数右边的数求两数之和,那么四...

  • 四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,...

  • 四数之和

    给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,...

  • 四数之和

    leetcode 18 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是...

  • 四数之和

    题目来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/4sum...

  • 四数之和

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/4sum[h...

  • 四数之和

    给你四个整数数组 nums1、nums2、nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 ...

  • 双指针总结

    左右指针 主要解决数组中的问题:如二分查找 盛最多水的容器 三数之和 四数之和 最接近三数之和 快慢指针 主要解决...

网友评论

      本文标题:四数之和

      本文链接:https://www.haomeiwen.com/subject/lukptqtx.html