码迷,mamicode.com
首页 > 其他好文 > 详细

三数之和

时间:2017-01-20 14:45:13      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:return   注意事项   iterator   包含   style   arp   let   ber   unique   

题目描述:

给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。

 注意事项

在三元组(a, b, c),要求a <= b <= c。

结果不能包含重复的三元组。

样例

如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集合的是:

(-1, 0, 1)

(-1, -1, 2)

 

解法1:三层for循环,时间复杂度O(n^3)

//解法1: O(n^3)
class Solution {
public:
    /**
     * @param numbers : Give an array numbers of n integer
     * @return : Find all unique triplets in the array which gives the sum of zero.
     */
    vector<vector<int> > threeSum(vector<int> &nums) {
        // write your code here
        vector<vector<int> > ans;
        int n = nums.size();
        map<vector<int>, int> mp;
        vector<int> temp;

        for (int i=0; i<n; ++i) {
            for (int j=i+1; j<n; ++j) {
                for (int k=j+1; k<n; ++k) {
                    if (nums[i]+nums[j]+nums[k] == 0) {
                       temp.resize(0);
                       temp.push_back(nums[i]);
                       temp.push_back(nums[j]);
                       temp.push_back(nums[k]);
                       temp.resize(3);
                       sort(temp.begin(), temp.end());
                       if (mp[temp]) {
                        continue;
                       }else {
                        mp[temp]++;
                        ans.push_back(temp);
                       }
                    }
                }
            }
        }
        return ans;
    }
};

 

解法2:将原数组备份,遍历每一个数val,两层for循环查找是否能找到两数之和为-val。注意:三个数相等时、或任意两数相等时的情况和去重。

时间复杂度O(n^2).

//解法2:O(n^2)

class Solution {
public:
    /**
     * @param numbers : Give an array numbers of n integer
     * @return : Find all unique triplets in the array which gives the sum of zero.
     */
    vector<vector<int> > threeSum(vector<int> &nums) {
        // write your code here
        vector<int> ori_num;
        map<int, int> cnt;
        for (int i=0; i<nums.size(); ++i) {
            ori_num.push_back(nums[i]);
            cnt[nums[i]]++;
        }
        vector<vector<int> > ans;
        sort(nums.begin(), nums.end());
        vector<int> temp;
        map<vector<int>, int> vector_mp;

        for (int i=0; i<nums.size(); ++i) {
            for (int j=i+1; j<nums.size(); ++j) {
                int tot = nums[i] + nums[j];
                auto iter = find(ori_num.begin(), ori_num.end(), 0-tot);
                if (nums[i] == 0-tot) {
                    if (cnt[nums[i]] < 2) continue;
                }
                if (nums[j] == 0-tot) {
                    if (cnt[nums[j]] < 2) continue;
                }
                if (nums[i] == nums[j] && nums[i] == 0-tot) {
                    if (cnt[nums[i]] < 3) continue;
                }
                if (iter != ori_num.end()) {
                    temp.resize(0);
                    temp = {0-tot, nums[i], nums[j]};
                    temp.resize(3);
                    sort(temp.begin(), temp.end());
                    if (vector_mp[temp] == 0)
                        ans.push_back(temp);
                        vector_mp[temp]++;
                }
            }
        }
        sort(ans.begin(), ans.end());
        return ans;
    }
};

 

解法3:对原数组从小到大排序,从左到右遍历val,然后二分查找两个数之和是-val的。左边界i+1,右边界n-1,如果当前和=-val,加入答案,否则如果小于,左边界右移,相反右边界左移。可以利用map去重,也可以利用set容器不能包含重复项的特点来去重。时间复杂度O(nlog(n)).

/// 解法3:对原数组从小到大排序,从左到右遍历,然后找两个数之和是-target的。
class Solution {
public:
    /**
     * @param numbers : Give an array numbers of n integer
     * @return : Find all unique triplets in the array which gives the sum of zero.
     */
    vector<vector<int> > threeSum(vector<int> &nums) {
        // write your code here
        sort(nums.begin(), nums.end());
        set<vector<int> > st;

        for (int i=0; i<nums.size(); ++i) {
            int val = nums[i];
            if (val > 0) break;
            int l = i + 1;
            int r = nums.size() - 1;
            while(l < r) {
                int temp = nums[l] + nums[r];
                if (temp == -val) {
                    st.insert({val, nums[l], nums[r]});
                    while(l<nums.size()-1 && nums[l+1] == nums[l]) l++;
                    while(nums[r-1] == nums[r] && r > 0) r--;
                }else if (temp > -val) r -= 1;
                else if (temp < -val) l += 1;
            }
        }
        vector<vector<int> > ans;
        set<vector<int> >::iterator iter;

        for (iter=st.begin(); iter != st.end(); ++iter) {
            ans.push_back(*iter);
        }
        return ans;
    }
};

  

三数之和

标签:return   注意事项   iterator   包含   style   arp   let   ber   unique   

原文地址:http://www.cnblogs.com/icode-girl/p/6322618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!