15. 3Sum - Medium

Given an array nums of n integers, are there elements abc in nums 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.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

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

sort the input array and then run through all indices of a possible first element of a triplet.

for each possible first element, use two pointers like in 2sum problem

skip equal elements to avoid duplicates in the answer without a set

注意:在skip same element的时候,l和r的方法是相反的,应该判断nums[l] = nums[l-1]和nums[r] = nums[r+1]

时间:O(N^2),空间:O(1)

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        for(int i = 0; i + 2 < nums.length; i++) {
            if(i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
                int l = i + 1, r = nums.length - 1, target = -nums[i];
                while(l < r) {
                    if(nums[l] + nums[r] == target) {
                        res.add(Arrays.asList(nums[i], nums[l], nums[r]));
                        l++;r--;
                        while(l < r && nums[l] == nums[l - 1])
                            l++;
                        while(l < r && nums[r] == nums[r + 1])
                            r--;
                    } else if(nums[l] + nums[r] > target) {
                        r--;
                    } else {
                        l++;
                    }
                }
            }
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10039480.html

时间: 2024-10-11 05:49:33

15. 3Sum - Medium的相关文章

Leetcode之15. 3Sum (medium)

15. 3Sum (medium) 描述 Given an array nums of n integers, are there elements a, b, c in nums 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. Example

1. Two Sum&amp;&amp;15. 3Sum&amp;&amp;18. 4Sum

题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Given nums = [2, 7, 1

[LeetCode] 015. 3Sum (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 015.3Sum (Medium) 链接: 题目:https://oj.leetcode.com/problems/3sum/ 代码(github):https://github.com/illuz/leetcode 题意: 在给定数列中找出三个数,使和为 0. 分析: 先排序,再左右夹逼,复杂度 O(n*n).

15 3Sum(寻找三个数之和为指定数的集合Medium)

题目意思:给一个乱序数组,在里面寻找三个数之和为0的所有情况,这些情况不能重复,增序排列 思路:前面2sum,我用的是map,自然那道题map比双指针效率高,这道题需要先排序,再给三个指针,i.j.k 对于i指针从前往后遍历,对于一个固定的i指针,其实就是2Sum的情况,给定一前一后两个指针进行遍历, 值大了,就把后面的指针往前移,值小了就把前面的指针往后移. 比较麻烦的地方在于去重,首先是i指针的去重,j和k只用一个去重就可以了 ps:这是数组中一种十分常用的方法. 1 class Solut

Leet Code OJ 15. 3Sum[Difficulty: Medium]

题目: 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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s

[LeetCode] 15. 3Sum

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,

15.3sum(Nsum总结)

如果不用hashmap解决2sum,而是把2sum看成nSum中的一部分,那么就需要用two pointers来解决问题. 2sum伪代码: 1. 先对给定的数组进行排序 2. walker指第一个元素,runner指最后一个元素 3.当walker小于runner: 1)如果walker和runner的和等于target,那么: (1)如果只需要一对解,记录答案,break (2)如果需要所有解,那么walker++, runner-- 2) else如果和小于target,walker++

【leetcode】15. 3Sum

题目描述: 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. 解题分析: 这道题注意一下几点即可: 1,先固定前两个数的位置,移动第三个数,这样的查找方式不会有数字组合的遗漏: 2,要考虑到数组元素有重复的情况下的处理. 3,若先为数组排

LeetCode 15. 3Sum(三数之和)

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,