Leetcode-Methodology-Preprocessing

本文探讨了如何找出数组中三个整数相加等于零的所有唯一组合。通过两种不同的算法实现,包括使用哈希表的方法和双指针技巧,解决了重复元素的问题,确保输出结果不含重复的三元组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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, 0, 1, 2, -1, -4],

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

Code 1

#include <iostream>
#include <list>
#include <string>
#include <iterator>
#include <iomanip>
#include <utility> 
#include <memory>
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>

#include <vector>


using namespace std;

typedef unordered_set<int> intset;

typedef pair<int, int> piit;

typedef unordered_multimap<int/*2sum*/, piit> intmap;
typedef unordered_multiset<int> intmset;
class Solution {

public:

    static
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        if (nums.size() < 3) return res;
        intmset mset;
        for (auto i : nums)mset.insert(i);
        /*special case */
        vector<int> tmp(3,0);
        {
            auto resf = mset.equal_range(0);
            int mc = 0;
            for (auto it = resf.first; it != resf.second; ++it)mc++;
            if (mc>=3)
                res.push_back(tmp);
        }
        /**/
        unordered_set<int> tset0, tset1;
        for (auto i : nums){
            if (i > 0)
                tset1.insert(i);
            else if(i!=0) tset0.insert(i);
        }
        vector<int> minusV(tset0.begin(), tset0.end()),
            plusV(tset1.begin(), tset0.end());
        for (int im = 0; im < minusV.size(); ++im) {
            for (int ip = 0; ip < plusV.size(); ++ip) {
                int t0 = 0 - minusV[im] - plusV[ip];
                auto resf = mset.equal_range(t0);
                tmp[0] = minusV[im];
                tmp[1] = t0;
                tmp[2] = plusV[ip];
                int mc = 0;
                for (auto it = resf.first; it != resf.second; ++it) {
                    mc++;
                }
                if (t0 == minusV[im] || t0 == plusV[ip]) {
                    if (mc >= 2) 
                        res.push_back(tmp);
                } else if (t0 > minusV[im] && t0 < plusV[ip]) {
                    if (mc >= 1) 
                        res.push_back(tmp);
                }
            }
        }
        return res;


    }// return

};

Code 2

vector<vector<int> > threeSum(vector<int> &num) {

    vector<vector<int> > res;

    std::sort(num.begin(), num.end());

    for (int i = 0; i < num.size(); i++) {

        int target = -num[i];
        int front = i + 1;
        int back = num.size() - 1;

        while (front < back) {

            int sum = num[front] + num[back];

            // Finding answer which start from number num[i]
            if (sum < target)
                front++;

            else if (sum > target)
                back--;

            else {
                vector<int> triplet(3, 0);
                triplet[0] = num[i];
                triplet[1] = num[front];
                triplet[2] = num[back];
                res.push_back(triplet);

                // Processing duplicates of Number 2
                // Rolling the front pointer to the next different number forwards
                while (front < back && num[front] == triplet[1]) front++;

                // Processing duplicates of Number 3
                // Rolling the back pointer to the next different number backwards
                while (front < back && num[back] == triplet[2]) rear--;
            }

        }

        // Processing duplicates of Number 1
        while (i + 1 < num.size() && num[i + 1] == num[i]) 
            i++;

    }

    return res;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值