LeetCode18 四数之和

问题描述:给定一个包含 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]]
思路

  • 暴力法

遍历所有情况找出所有满足情况的数组,用先对满足的数字序列排序来达到去重的目的。
时间复杂度:o(n4)
空间复杂度:o(n)

  • 排序+指针

与三数之和类似,先排序,对前两个元素进行遍历,在遍历过程中设置后续两个左右指针,通过与target的比较情况来进行移动。
时间复杂度:o(n3)
空间复杂度:o(1)

//java
//暴力法
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        if(nums.length < 4) return new ArrayList();
        Arrays.sort(nums);//先排序,方便后续去重
        List<List<Integer>> list = new ArrayList<>();
        for(int i = 0; i < nums.length; ++i){
            int targeti = target - nums[i];
            for(int j = i + 1; j < nums.length; ++j){
                for(int k = j + 1; k < nums.length; ++k){
                    for(int p = k + 1; p < nums.length; ++p){
                        if(nums[j] + nums[k] + nums[p] == targeti){
                            List<Integer> ls = new ArrayList<Integer>(Arrays.asList(nums[i], nums[j], nums[k], nums[p]));
                            if(!list.contains(ls)) list.add(ls);//去重
                        }
                    }
                }
            }
        }
        return list;
    }
}

//排序+指针
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        if(nums.length < 4) return new ArrayList();
        Arrays.sort(nums);
        List<List<Integer>> list = new ArrayList<>();
        int len = nums.length;
        for(int i = 0; i < len-3; ++i){
            if(i > 0 && nums[i] == nums[i-1]) continue;//相同元素则跳过,去重
            //优化,一旦出现后续遍历不可能满足的情况,立即剪枝
            int min = nums[i] + nums[i+1] + nums[i+2] + nums[i+3];
            if(min > target)    break;//最小情况大于目标值,跳过后续所有遍历
            int max = nums[i] + nums[len-1]+nums[len-2]+nums[len-3];
            if(max < target)    continue;//最大情况小于目标值,在该位置上的后续指针移动不可能满足,跳过当前位置
            for(int j = i + 1; j < len-2; ++j){
                if(j > i+1 && nums[j] == nums[j-1]) continue;//相同元素则跳过
                int left = j + 1;
                int right = nums.length - 1;
                //优化
                int min1 = nums[i] + nums[j] + nums[j+1] + nums[j+2];
                if(min1 > target)    break;
                int max1 = nums[i] + nums[j]+nums[right]+nums[right-1];
                if(max1 < target)    continue;
                while(left < right){
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum == target){
                    list.add(new ArrayList<Integer>(Arrays.asList(nums[i],nums[j], nums[left], nums[right])));
                    //跳过重复值
                    while(left < right && nums[left] == nums[++left]){}
                    while(left < right && nums[right] == nums[--right]){}
                }else if(sum > target)
                    --right;
                else
                    ++left;
                }
            }
        }
        return list;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Leo木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值