前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >每天一道leetcode18-四数之和

每天一道leetcode18-四数之和

作者头像
乔戈里
发布2019-09-17 15:04:02
2970
发布2019-09-17 15:04:02
举报
文章被收录于专栏:Java那些事

昨天的题解

题目

每天一道leetcode18- 四数之和 分类:双指针 中文链接: https://leetcode-cn.com/problems/4sum/description/ 英文链接 https://leetcode.com/problems/4sum/description/

题目详述

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

题目详解

思路

  • 这个题目和昨天的挺像的,就在昨天的代码改了改就AC了
  • 先sort一下array,为啥要sort呢,因为要用到two pointers 来遍历找两数之和,只有在从小到大排序之后的结果上,才能根据情况移动left 和right。
  • 首先是如果数组只有4个数字,直接判断4个数的和是不是target;(最少4个数)
  • 当确定好了第一个数字和第二个数字以后,如何确定?一个从开头开始,begin,是外层循环,一个从末尾往前遍历,end,是内层循环(要注意,必须间隔两个数,因为要取4个数)
  • 就在剩下的array里找找两个数,left从begin+1开始,right从end-1开始往前遍历,取4个数的和得到tempSum,比较这个tempSum是不是等于target,如果是target,则把这个结果保存下来(这里注意先把结果存到hashSet里面,去重)
  • 之后如果tempSum与target不相等 利用two pointers 特性, 如果tempSum 比target 小的话,说明我们需要更大的sum,所以要让left++以便得到更大的sum。 如果tempSum 比target 大的话,我们就需要更小的sum,所以right--。
代码语言:javascript
复制
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        HashSet<List<Integer>> set = new HashSet<>();
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length == 4)
        {
            if(nums[0] + nums[1] + nums[2] + nums[3] == target)
            {
                List<Integer> temp = new ArrayList<>();
                temp.add(nums[0]);
                temp.add(nums[1]);
                temp.add(nums[2]);
                temp.add(nums[3]);
                result.add(temp);
                return result;
            }
            return result;
        }
        Arrays.sort(nums);
        for(int begin = 0;begin < nums.length-3;begin++)
        {
            for(int end = nums.length-1;end - begin > 2;end--)
            {
                int left = begin + 1;
                int right = end - 1;
                while(left < right)
                {
                    int tempSum = nums[begin] + nums[end] + nums[left] + nums[right];
                    if(tempSum == target)
                    {
                        List<Integer> temp = new ArrayList<>();
                        temp.add(nums[begin]);
                        temp.add(nums[left]);
                        temp.add(nums[right]);
                        temp.add(nums[end]);
                        set.add(temp);
                        left++;
                    }else if(tempSum < target)
                        left++;
                    else
                        right--;
                }
            }
        }
        for(List<Integer> t: set)
            result.add(t);
        return result;
    }
}

代码讲解

  • 5-18行就是如果数组长度是4,那么直接判断数组和是不是target,如果是target则返回这个,不是返回空
  • 19行对nums进行排序
  • 20-22行就是先确定好两个数,一个begin一个end,注意间隔end-begin>2的哦~
  • 26-37行就是判断这4个数的和tempSum是不是target,如果是target那么就填入到hashset中
  • 38-41行就是之后如果tempSum与target不相等 利用two pointers 特性, 如果tempSum 比target小的话,说明我们需要更大的sum,所以要让left++以便得到更大的sum。 如果tempSum 比0 大的话,我们就需要更小的sum,所以right--。
  • 45-47行 遍历这个set然后加入到List中,把它返回。

结束语

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-11-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序员乔戈里 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 昨天的题解
    • 题目
      • 题目详述
        • 题目详解
          • 结束语
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档