Leetcode-Methodology-Preprocessing

来源:互联网 发布:淘宝销售属性虚拟类目 编辑:程序博客网 时间:2024/06/07 14:42

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;}
0 0
原创粉丝点击