15. 3Sum

題目

Given an array S of n integers, are there elements abc 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]
]
分析

本題其實是找到兩個和爲特定值的組合的變形,即先確定第一個元素,然後問題就變成了在剩餘元素中尋找兩個和爲第一個元素的負數的組合,由於題目要求找到所有的三元組,且三元組之間不重複,則代表第一個元素確定之後,與其值相同的後續元素都不能再作爲第一元素出現,同樣,當後兩個元素的值確定之後,不能再出現相同的兩個元素,所以先對整個數組排序,然後在尋找三元組的過程中去重。

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        vector<vector<int>> res;
        sort(nums.begin(),nums.end());//先將數組排序
        int size=nums.size();
        if(size<3)
            return res;
        for(int i=0;i<size-2;++i){//選定第一個元素爲nums[i],下面就是從後續中找到兩個元素之和爲-nums[i]
            if(nums[i]>0)//當第一個元素大於0時,由於已經排序,不可能出現三元組之和等於0
                break;
            int target=-nums[i];
            int beg=i+1;//從i+1到size-1之間查找
            int end=size-1;
            while(beg<end){
                int sum=nums[beg]+nums[end];//查看sum與target之間的關係,小於則代表nums[beg]太小,大於則代表nums[end]過大,通過不斷調整使得sum逼近target
                if(sum<target)
                    ++beg;
                else if(sum>target)
                    --end;
                else{//當sum=target時,構造三元組作爲一個解,同時調整beg和end,忽略掉二者的重複元素,然後再查找下一個可能的解
                    res.push_back({nums[i],nums[beg],nums[end]});
                    while(beg<end&&nums[beg]==nums[beg+1])
                        ++beg;
                    while(beg<end&&nums[end]==nums[end-1])
                        --end;
                    ++beg;
                    --end;
                }
            }
            while(i+1<size&&nums[i+1]==nums[i])//以nums[i]爲第一個元素的三元組查找完畢,下一步是忽略掉nums[i]的重複元素
                ++i;
        }
        return res;
    }
};
參考文章:Share my AC C++ solution, around 50ms, O(N*N), with explanation and comments

發佈了180 篇原創文章 · 獲贊 15 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章