18. 四数之和

18. 四数之和

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

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

示例 1:

输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

示例 2:

输入:nums = [], target = 0
输出:[]

提示:

  • 0 <= nums.length <= 200
  • -109 <= nums[i] <= 109
  • -109 <= target <= 109
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        if(nums.length < 4){
            return result;
        }
        int fixed1, fixed2, move1, move2, tmp = 0, min, max;
        // 排序
        Arrays.sort(nums);
        // 先计算整个数列的总体范围,如果target不在范围内,直接返回
        min = nums[0] + nums[1] + nums[2] + nums[3];
        max = nums[nums.length - 4] + nums[nums.length - 3] + nums[nums.length - 2] + nums[nums.length - 1];
        if(target < min || target > max){
            return result;
        }
        for(fixed1 = 0; fixed1 < nums.length - 3; ++fixed1){
            if(fixed1 != 0 && nums[fixed1 - 1] == nums[fixed1]){
                continue;
            }
            // 因为fixed1已经固定了,所以当前轮有一个数已经确定,直接对另外三个数字找最大和最小的情况,就能知道范围。但是不能直接return,因为下一轮的时候fixed1就发生了变化,所以极值范围也就发生了变化。
            min = nums[fixed1] + nums[fixed1 + 1] + nums[fixed1 + 2] + nums[fixed1 + 3];
            max = nums[fixed1] + nums[nums.length - 3] + nums[nums.length - 2] + nums[nums.length - 1];
            if(target < min || target > max){
                continue;
            }
            for(fixed2 = fixed1 + 1; fixed2 < nums.length - 2; ++fixed2){
                if(fixed2 != fixed1 + 1 && nums[fixed2 - 1] == nums[fixed2]){
                    continue;
                }
                // 因为fixed1和2已经固定了,所以当前轮有两个数已经确定,直接对另外两个数字找最大和最小的情况,就能知道范围。但是不能直接return,因为下一轮的时候fixed2就发生了变化,所以极值范围也就发生了变化。
                min = nums[fixed1] + nums[fixed2] + nums[fixed2 + 1] + nums[fixed2 + 2];
                max = nums[fixed1] + nums[fixed2] + nums[nums.length - 2] + nums[nums.length - 1];
                if(target < min || target > max){
                    continue;
                }
                for(move1 = fixed2 + 1, move2 = nums.length - 1; move1 < move2; ++move1){
                    if(move1 != fixed2 + 1 && nums[move1 - 1] == nums[move1]){
                        continue;
                    }
                    tmp = nums[fixed1] + nums[fixed2] + nums[move1] + nums[move2];
                    while(move1 < move2){
                        if(tmp > target){
                            tmp = tmp - nums[move2] + nums[move2 - 1];
                            --move2;
                        } else {
                            break;
                        }
                    }
                    if(move1 >= move2){
                        break;
                    }
                    if(tmp == target){
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[fixed1]);
                        temp.add(nums[fixed2]);
                        temp.add(nums[move1]);
                        temp.add(nums[move2]);
                        result.add(temp);
                    }
                }
            }
        }
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值