资讯专栏INFORMATION COLUMN

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

Yangyang / 433人阅读

摘要:题目要求输入一个整数数组,从中找到所有的三个整数组成一个数组,这三个整数的和为。要求不包括重复的结果思路一无这里使用了三个指针。先对数组进行排序。

题目要求
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.

Note: The solution set must not contain duplicate triplets.

For example, given array S = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

输入一个整数数组,从中找到所有的三个整数组成一个数组,这三个整数的和为0。要求不包括重复的结果

思路一:无hashset or hashmap

这里使用了三个指针。
先对数组进行排序。确定左侧固定的指针,然后移动右侧两个直至找到三个值和为0.如果当前三个指针的值小于0,则将中间的指针右移至下一个不同的值,如果小于0,则将最右侧指针左移至下一个不重复的值。一旦右侧和中间的指针重合,移动左侧指针至下一个不重复的值,并且初始化中间和右侧的指针

    public List> threeSum2(int[] nums) {
           List> result = new ArrayList>();
        int length = nums.length;
        if(length<3){
            return result;
        }
        Arrays.sort(nums);

        int i = 0;
        while(i0) break;
            int j = i+1;
            int k = nums.length - 1;
            while(j=0){
                    //消去右侧重复的数字
                    while(nums[k--] == nums[k] && j < k);
                }
                
                //消去和当前左指针相同的数字
                while(nums[i] == nums[++i] && i < nums.length - 2);
            }
            
        }
        return result;
    }
思路二:有hashmap/hashset

利用hashmap/hashset是为了避开重复的值,但是效率值明显不如上一种方法高

public List> threeSum(int[] num) {
    Arrays.sort(num);
    List> list = new ArrayList>();
    HashSet> set = new HashSet>();
    for(int i=0;i l= new ArrayList();
                l.add(num[i]);
                l.add(num[j]);
                l.add(num[k]);
                if(set.add(l))
                list.add(l);
                j++;
                k--;
            }
            else if(num[i]+num[j]+num[k]<0)
            j++;
            else
            k--;
        }
    }
    return list;
}


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

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

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

相关文章

  • 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 15 3Sum

    摘要:要求序列不重复。这个问题比较复杂的一点是,还要处理重复的数据。为了简化我们的操作,我们先对数组进行预排序。排序之后,对于求两个数和的问题,可以通过和两个指针从两边查找,也简化了操作时间。解法防止重复序列 题目详情 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0...

    FuisonDesign 评论0 收藏0
  • leetcode494. Target Sum

    摘要:为了寻找合适的正负号赋值,我们其实可以将数组分为两个子集,其中一个子集中的数字都被赋予了正号,而另一个子集中的数字都被赋予了负号。如果二者的和不是一个偶数,就一定无法找到这样的正负号集合使得其结果为。 题目要求 You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now yo...

    RobinTang 评论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
  • [LintCode/LeetCode] 3Sum

    摘要:双指针法的解法。然后用和夹逼找到使三数和为零的三数数列,放入结果数组。对于这三个数,如果循环的下一个数值和当前数值相等,就跳过以避免中有相同的解。 Problem Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplet...

    Sunxb 评论0 收藏0

发表评论

0条评论

Yangyang

|高级讲师

TA的文章

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