【leetcode】15. 3Sum

230 篇文章 0 订阅

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

题目链接:https://leetcode.com/problems/3sum/

 

这道题是1. Two Sum的升级版,3个数相加为指定和。

常规思路是按照2数和的方法,排序,固定一个数字,找剩余2数和为指定数的组合。特别的,由于本题指定和为0,说明满足要求的3个数要么全为0,要么有正有负,因此在固定数字的遍历时,可以只需要遍历非正的(或者非负),就能找到所有解。

去重的几个点:

1)相同固定数字得到的解一样,直接跳过;

2)同一固定数字已找到一个解,继续找是否有别的解时,要先跳过与当前解相同的数字。

 

代码:

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        int len = nums.size();
        if (len <= 0) return {};
        sort(nums.begin(), nums.end());
        int i = 0;
        while (i < len && nums[i] <= 0){
            if (i > 0 && nums[i] == nums[i - 1]){
                ++i;
                continue;
            }
            int target = 0 - nums[i];
            int j = i + 1, k = len - 1;
            while (j < k){
                if (nums[j] + nums[k] == target){
                    res.push_back({nums[i], nums[j], nums[k]});
                    while (j < k && nums[j] == nums[j + 1]) ++j;
                    while (j < k && nums[k] == nums[k - 1]) --k;
                    ++j;
                    --k;
                }else if(nums[j] + nums[k] > target) --k;
                else ++j;
            }
            ++i;
        }
        return res;
    }
};

关于双指针的小tip:

找组合的题和找区间类似,双指针能降低时间复杂度。数字组合通常要排序来辅助。

 

本题c++ vector的tip:

vector的初始化可以为

std::vector<type> name {value1, value2, ...};

本题里直接将匿名初始化的vector加入大vector集中。

 

类似思想的题:

1. Two Sum

16. 3Sum Closest

18. 4Sum

 

本篇参考:http://www.cnblogs.com/grandyang/p/4481576.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值