LeetCode-18 四数之和

  • 题目:18. 四数之和
  • 难度:中等
  • 分类:数组
  • 解决方案:双指针

今天我们学习第18题四数之和,这是一道中等题。像这样数组的题目经常作为面试题来考察面试者算法能力和写代码能力,因此最好能手写出该题。下面我们看看这道题的题目描述。

题目描述

给定一个包含n个整数的数组nums和一个目标值target,判断nums 中是否存在四个元素abcd ,使得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]
]

分析

在前面,我们做过LeetCode-1 两数之和LeetCode-15 三数之和,这个题目是求四数之和,其基本思路和三数之和差不多,只是在三数之和的基础上增加一个for循环。具体求解过程详见LeetCode-15 三数之和java代码如下所示;

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(nums == null && nums.length < 4)
            return res;
        
        // 对数组排序
        Arrays.sort(nums);
        
        // 固定第一个数
        for(int i=0; i<nums.length-3; ++i){
            
            if(i!=0 && nums[i] == nums[i-1]) 
                continue;
            
            if(nums[i+1]>0 && nums[i] >= target)
                return res;
            // 固定第二个数
            for(int j=i+1; j<nums.length-2; ++j){
                if((j != i + 1) && nums[j] == nums[j-1]) 
                    continue;
                
                // 使用左右指针进行查找
                int left = j+1, right = nums.length-1;
                while(left < right){
                    int sum = nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum == target){
                        ArrayList<Integer> tmp = new ArrayList<Integer>();
                        tmp.add(nums[i]);
                        tmp.add(nums[j]);
                        tmp.add(nums[left]);
                        tmp.add(nums[right]);
                        res.add(tmp);
                        ++left;
                        --right;
                        while(left<right && nums[left] == nums[left-1])
                            ++left;
                        while(left<right && nums[right] == nums[right+1])
                            --right;
                    }else if(sum > target)
                        --right;
                    else
                        ++left;
                }
            }
        }
        return res;
    }
}
5600819-4fcafdbfb0069f80.png
提交结果

Github地址

LeetCode-18 四数之和

参考链接

四数之和

5600819-ae296da395ceb040.jpg
更多文章,请扫描二维码关注『算法半岛』

猜你喜欢

转载自blog.csdn.net/weixin_33965305/article/details/90821569