[leetcode]15. 3Sum

556 篇文章 2 订阅
441 篇文章 0 订阅

Description

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]
]

分析

  • 这个题目的解法我觉得是个经典的解法,先排序,然后再遍历暴力找出来。
  • 由于题目给出不能含有重复项,我们很容易想到用set集合来做,最后变成vector就行了。

C++ 代码

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> result;
        if(nums.size()<3){
            return result;
        }
        set<vector<int>> temp;
        sort(nums.begin(),nums.end());
        for(int i=0;i<nums.size()-2;i++){
            int left=i+1;
            int right=nums.size()-1;
            while(left<right){
                int sum=nums[left]+nums[right]+nums[i];
                if(sum==0){
                    vector<int> ans={nums[i],nums[left],nums[right]};
                    temp.insert(ans);
                    left++;
                    right--;
                }else if(sum<0){
                    left++;
                }else{
                    right--;
                }
            }
        }
        return vector<vector<int>> (temp.begin(),temp.end());
    }
};

Python代码

这道题的主要难点就是在去重上面,稍微不注意重复,就容易翻车。

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        res=[]
        nums.sort()
        for i in range(len(nums)):
            if i>0 and nums[i]==nums[i-1]: 
                continue  
            left = i+1
            right = len(nums)-1
            while(left<right):
                val = nums[i]+nums[left]+nums[right]
                if(val==0):
                    res.append([nums[i],nums[left],nums[right]])
                    pre_l = left
                    while left<right and nums[left]==nums[pre_l]:
                        left+=1
                    right-=1
                elif val>0:
                    right-=1
                else:
                    left+=1
        return res

Python

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        # two pointer
        nums.sort()
        n = len(nums)
        res = []
        for i in range(len(nums)-2):
            if i >0 and nums[i]==nums[i-1]: # 核心判断重复
                continue
            left = i+1
            right = n-1
            while left<right:
                val=nums[i]+nums[left]+nums[right]
                if val==0:
                    cur_nums = [nums[i], nums[left],nums[right]]
                    res.append(cur_nums)
                    while left<right and nums[left+1]==nums[left]: # 核心判断重复
                        left+=1
                    while left<right  and nums[right]==nums[right-1]: # 核心判断重复
                        right-=1
                    left+=1
                    right-=1
                elif val>0:
                    right-=1
                else:
                    left+=1
        return res

参考文献

[编程题]3sum

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值