首页 > 其他分享 >15. 三数之和

15. 三数之和

时间:2022-11-10 00:03:00浏览次数:63  
标签:right 15 nums int 三数 三元组 result left

15. 三数之和

给你一个整数数组 nums ,判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i != ji != kj != k ,同时还满足 nums[i] + nums[j] + nums[k] == 0 。请

你返回所有和为 0 且不重复的三元组。

注意:答案中不可以包含重复的三元组。

示例 1:

输入:nums = [-1,0,1,2,-1,-4]
输出:[[-1,-1,2],[-1,0,1]]
解释:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。
不同的三元组是 [-1,0,1] 和 [-1,-1,2] 。
注意,输出的顺序和三元组的顺序并不重要。

示例 2:

输入:nums = [0,1,1]
输出:[]
解释:唯一可能的三元组和不为 0 。

示例 3:

输入:nums = [0,0,0]
输出:[[0,0,0]]
解释:唯一可能的三元组和为 0 。

提示:

  • 3 <= nums.length <= 3000
  • -105 <= nums[i] <= 105

解法:第一个数用nums[i]记录,并用i进行控制遍历;后两个数分别用快慢指针进行记录(left指向i+1位置,right指向nums.length-1)

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);

        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > 0) {
                return result;
            }
       // 去除重复的三元组
            if (i > 0 && nums[i] == nums[i - 1]) {
                continue;
            }

            int left = i + 1;
            int right = nums.length - 1;
            while (right > left) {
                int sum = nums[i] + nums[left] + nums[right];
                if (sum > 0) {
                    right--;
                } else if (sum < 0) {
                    left++;
                } else {
                    result.add(Arrays.asList(nums[i], nums[left], nums[right]));

                    while (right > left && nums[right] == nums[right - 1]) right--;
                    while (right > left && nums[left] == nums[left + 1]) left++;
                    
                    right--; 
                    left++;
                }
            }
        }
        return result;
    }
}

 

标签:right,15,nums,int,三数,三元组,result,left
From: https://www.cnblogs.com/fulaien/p/16875625.html

相关文章

  • URAL 1517 Freedom of Choice
    DescriptionBackgroundBeforeAlbanianpeoplecouldbearwiththefreedomofspeech(thisstoryisfullydescribedintheproblem "Freedomofsp......
  • LuoguP1586 题解
    也可以在LuoguP1586(tencentcs.com)获得更好的阅读体验。Luogu_P1586题解一道比较简单的题目,看到求种类数,考虑DP。设\(f_{i,j}\)表示第\(i\)个数划分为\(j\)......
  • HDU 2715 Herd Sums
    DescriptionThecowsinfarmerJohn'sherdarenumberedandbrandedwithconsecutiveintegersfrom1toN(1<=N<=10,000,000).Whenthecowscometot......
  • HYSBZ 1588 营业额统计
    Description营业额统计Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况。Tiger拿出了公司的账本,账本上记录了......
  • HDU 1542 Atlantis
    ProblemDescriptionThereareseveralancientGreektextsthatcontaindescriptionsofthefabledislandAtlantis.Someofthesetextsevenincludemaps......
  • leetcode-1556-easy
    ThousandSeparatorGivenanintegern,addadot(".")asthethousandsseparatorandreturnitinstringformat.Example1:Input:n=987Output:"987"Exa......
  • HUST 1599 Multiple
    DescriptionRocket323 lovesmathverymuch.Oneday, Rocket323 gotanumberstring.Hecouldchoosesomeconsecutivedigitsfromthestringtoform......
  • day15
    [0015.三数之和]首先哈希法我不会用就算看了思路和代码还是不会尤其是去重那块其次看到可以用双指针法那我又去想了想想自己动手写但还是写不出来然后......
  • 设计模式利剑15-组合模式
    定     义:将对象组合成树形结构以表示“整体-部分”的层次结构,使得用户对单个对象和组合对象的使用具有一致性优     点:              1、高层......
  • 问题 L: 零基础学C/C++157——保留尾部*
    该题与前面的删除前导一样,之前我们是找到第一个不是的字符,那么现在一样的,我们可以从后往前找,找到第一个不是的字符将其前面的删除(不输出)点击查看代码#include<stdio.h>......