美文网首页
四数之和

四数之和

作者: 二进制的二哈 | 来源:发表于2019-11-30 14:13 被阅读0次

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

给定一个包含 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]
]

思路:这道题可以在“三数之和”的基础解决。

Java代码:

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        if(nums != null && nums.length > 3){
            //对数组排序
            Arrays.sort(nums);//[-2,-1,0,0,1,2]
            for(int i = 0;i<nums.length;i++){
                int curNum = nums[i];
                //重复的数不用计算
                if(i > 0 && nums[i-1] == curNum) continue;
                int newTarget = target - curNum;
                findThrSum(ans,nums,newTarget,i);
            }
        }
        return ans;
    }


    /**
    *   找三数之和
    */
    private void findThrSum(List<List<Integer>> ans,int[] nums,int target,int startIndex){
        if(nums.length - startIndex < 4)
            return;
        for(int i = startIndex + 1;i<nums.length;i++){
            int curNum = nums[i];
            //重复的数不用计算
            if(i > startIndex + 1 && nums[i-1] == curNum) continue;
            int left = i + 1;
            int right = nums.length - 1;
            while(left < right){
                int sum = curNum + nums[left] + nums[right];
                if(sum == target){
                    //找到了
                    List<Integer> tmpList = new ArrayList<>();
                    tmpList.add(nums[startIndex]);
                    tmpList.add(curNum);
                    tmpList.add(nums[left]);
                    tmpList.add(nums[right]);
                    ans.add(tmpList);
                    //left向后找,如果数一样,就向后移动,达到去重的目的
                    while(left < right && nums[left] == nums[left+1]){
                        left++;
                    }
                    //right向前,如果数一样,就向前移动
                    while(left < right && nums[right] == nums[right-1]){
                        right--;
                    }
                    left++;
                    right--;
                }else if(sum < target){
                    //总和小于target,说明left的值太小了,left指针向右移动
                    left++;
                }else{
                    //总和大于target,说明right的值太大了,right指针向左移动
                    right--;
                }
            }
        }
    }
}

相关文章

  • 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/tusowctx.html