23.3Sum(三数和为零)

Level:

??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:

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

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

思路分析:

?求数组中三数和为零的情况,首先我们将数组进行排序,然后遍历数组,设置两个指针left和right,访问到节点i的时候,将left设置为i+1,right设置为nums.length-1。然后看nums[left]和nums[right]的和是否为-nums[i],如果是,那么就找到一组满足要求的解,如果不能则移动left和right,直到相等。

? 注意要避免重复的情况

代码:

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>>res=new ArrayList<>();
        if(nums==null||nums.length<3)
            return res;
        Arrays.sort(nums);  //进行排序
        for(int i=0;i<nums.length;i++){
            int towsum=-nums[i];
            int left=i+1;
            int right=nums.length-1;
            while(left<right){
                if(nums[left]+nums[right]==towsum){
                    res.add(Arrays.asList(nums[i],nums[left],nums[right]));
                    left++;
                    while(left<nums.length&&nums[left]==nums[left-1])
                        left++;               //避免出现重复情况
                    right--;
                    while(right>=0&&nums[right]==nums[right+1])
                        right--;              //避免出现重复情况
                }
                else if(nums[left]+nums[right]<towsum){
                    left++;
                }
                else{
                    right--;
                }
            }
            while(i<nums.length-1&&nums[i]==nums[i+1])
                i++;              //避免出现重复情况
        }
        return res;
    }
}

原文地址:https://www.cnblogs.com/yjxyy/p/10744600.html

时间: 2024-11-10 13:20:03

23.3Sum(三数和为零)的相关文章

【LeetCode每天一题】3Sum(三数之和)

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: Given array nums =

[Lintcode 3sum]三数之和(python,二分)

题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true# 用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了,关键是!!python的语法… 要想给tuple排序,如果直接sort的话会自动转成list,这个时候要再转回来. 1 class Solution: 2 """ 3 @param numbersbers : Give an array numbersbers of n inte

[LintCode] 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. Notice Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solu

3sum 三数之和为0的简单实现

思路简单: (1) 三重 for ,符合a+b+c=0的 a,b,c保存在tuple里 (2)tuple保存在set 中,一可去重,二可保持字典序 (3)简单代价就是复杂度很高,O(n^3*logn) typedef tuple<int,int,int> triplet; triplet sort3(int a,int b,int c){ if(a<=b){ if(b>c){ swap(b,c); if(a>b) swap(a,b); } } else{ swap(a,b);

[LeetCode] 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: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

[LeetCode] 3Sum Closest 最近三数之和

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

[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, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

LeetCode OJ:3Sum Closest(最接近的三数之和)

Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2

【算法练习题】力扣练习题——数组(2):三数之和

原题说明: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:[[-1, 0, 1],[-1, -1, 2]] 原题链接:https://leetcode-cn.com/problems/3sum 解法一:基于HashMap的暴力求解 参考力扣题