[LeetCode 15] 3Sum

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

分析 

寻找数组中的3个数使得其和为0, a+ b+ c = 0。这道题可以分解成,在数组寻找两个数a,b,使得其和等于-c, a+b = -c。

首先需要对数组进行排序,这样可以比较快的找到两个数。然后从头至尾遍历数组, i start from 0 to nums.size(), 为了防止结果重复,需要在[i, nums.size())中找到这两个数,这样只要nums[i]之前已经查找过了,之后的结果中就不会再出现nums[i]了。

需要考虑一种情况,nums[i] = nums[i-1]时,同样会造成结果的重复,因此遍历时,当遇到这种情况直接跳过即可。

在寻找a,b两个数的过程中,同样需要防止结果重复。举例来说,nums排好序之后是这样的

nums:[-4, -1, 0, 1, 1]

nums[1] = -1

nums[2] + nums[3] = - nums[1]

nums[2] + nums[4] = - nums[1]

所以在寻找a,b的时候如果发现已经存在了相同的两元数组时,需要跳过。

 

Code

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        std::sort (nums.begin(), nums.end());
        int length = nums.size();
        vector<vector<int>> res;
        
        int prevInt = INT_MAX;
        for (int i =0; i < length; i ++)
        {
            if (prevInt == nums[i])
            {
                continue;
            }
            prevInt = nums[i];
            vector<vector<int>> twoSumRes = twoSum(nums, i+1, 0 - nums[i]);
            for (int j = 0; j < twoSumRes.size(); j ++)
            {
                vector<int> tmp;
                tmp.push_back(nums[i]);
                tmp.push_back(twoSumRes[j][0]);
                tmp.push_back(twoSumRes[j][1]);
                res.push_back(tmp);
            }
        }
        return res;
        
    }
    
    vector<vector<int>> twoSum(vector<int>& nums, int start, int sum)
    {
        int length = nums.size();
        int i = start;
        int j = length - 1;
        vector<vector<int>> res;
        
        vector<int> tmp;
        tmp.push_back(INT_MAX);
        tmp.push_back(INT_MAX);
        while (i < j)
        {
            if (nums[i] + nums[j] == sum && nums[i] != tmp[0] && nums[j] != tmp[1])
            {
                tmp[0] = nums[i];
                tmp[1] = nums[j];
                res.push_back(tmp);
            }
            else if (nums[i] + nums[j] > sum)
            {
                j --;
                continue;
            }
            else if (nums[i] + nums[j] < sum)
            {
                i ++;
                continue;
            }
        }
        return res;
    }
};

运行效率

Runtime: 128 ms, faster than 52.46% of C++ online submissions for 3Sum.

Memory Usage: 24.4 MB, less than 18.54% of C++ online submissions for3Sum. 

总体来看运行效率并没有很高。空间的原因可能在于twoSum中使用了vector做暂存,这样会增加copy和内存的使用,直接在threeSum中做遍历和查找,这样会对空间有比较大的节约,但是可读性不好。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值