资讯专栏INFORMATION COLUMN

leetcode 15 3Sum

FuisonDesign / 635人阅读

摘要:要求序列不重复。这个问题比较复杂的一点是,还要处理重复的数据。为了简化我们的操作,我们先对数组进行预排序。排序之后,对于求两个数和的问题,可以通过和两个指针从两边查找,也简化了操作时间。解法防止重复序列

题目详情
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

给定一个整数数组s,我们要找出s中是否有三个不同元素的加和等于0,如果有,输出所有满足条件的序列。要求序列不重复。

For example, 输入数组S = [-1, 0, 1, 2, -1, -4],
返回的结果集应该是:
[
[-1, 0, 1],
[-1, -1, 2]
]

想法

通过减治的思想,我们可把三个数求和的问题,减治为对于数组中不重复的元素nums[i],求两个数的和等于-nums[i]的过程。

这个问题比较复杂的一点是,还要处理重复的数据。为了简化我们的操作,我们先对数组进行预排序。

经历了预排序,我们判断一个元素是否重复,只需要比较它和之前位置的元素是否相等就可以了。

排序之后,对于求两个数和的问题,可以通过low和high两个指针从两边查找,也简化了操作时间。

解法
    public List> threeSum(int[] nums) {
        int length = nums.length;
         List> res = new  ArrayList>();
         Arrays.sort(nums);
        
         for(int i=0;i0 && nums[i] != nums[i-1])){
                 int low = i+1;
                 int high = length-1;
                 int target = -nums[i];
                 
                 while(low < high){
                     if(nums[low]+nums[high] == target){
                         res.add(Arrays.asList(nums[i], nums[low], nums[high]));
                         while(low < high && nums[low] == nums[low+1])low++;
                         while(low < high && nums[high] == nums[high-1])high--;
                         low++;
                         high--;
                     }else if(nums[low]+nums[high] < target){
                         low++;
                     }else{
                         high--;
                     }
                 }
             }
         }
        
        
        return res;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70984.html

相关文章

  • leetcode15 3Sum 从数组中找到三个整数,它们的和为0

    摘要:题目要求输入一个整数数组,从中找到所有的三个整数组成一个数组,这三个整数的和为。要求不包括重复的结果思路一无这里使用了三个指针。先对数组进行排序。 题目要求 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets i...

    Yangyang 评论0 收藏0
  • leetcode16 3Sum Closest

    摘要:返回这三个值的和。思路一三指针这里的思路和是一样的,就是用三个指针获得三个值并计算他们的和。 题外话 鉴于这一题的核心思路和leetcode15的思路相同,可以先写一下15题并参考一下我之前的一篇博客 题目要求 Given an array S of n integers, find three integers in S such that the sum is closest to...

    Blackjun 评论0 收藏0
  • [Leetcode] Two Sum, 3Sum,4Sum,4SumII,3Sum Closet

    摘要:解题思路题目要求两个数和等于,返回其题目说明不会有重复情况,所以我们一旦发现符合情况的,就可以直接结束循环并返回。特殊情况就是正好等于,那肯定是最接近的情况,直接返回即可。 Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific ta...

    EddieChan 评论0 收藏0
  • leetcode 16 3Sum Closest

    摘要:题目详情给定一个整数数组,我们需要找出数组中三个元素的加和,使这个加和最接近于输入的数值。返回这个最接近的加和。找后两个元素的时候,使用左右指针从两端查找。 题目详情 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, targe...

    atinosun 评论0 收藏0
  • [Leetcode] 3Sum 4Sum 3Sum Closet 多数和

    摘要:为了避免得到重复结果,我们不仅要跳过重复元素,而且要保证找的范围要是在我们最先选定的那个数之后的。而计算则同样是先选一个数,然后再剩下的数中计算。 2Sum 在分析多数和之前,请先看Two Sum的详解 3Sum 请参阅:https://yanjia.me/zh/2019/01/... 双指针法 复杂度 时间 O(N^2) 空间 O(1) 思路 3Sum其实可以转化成一个2Sum的题,...

    trigkit4 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<