OJ Practice 48--t15 3Sum

Source: Internet
Author: User

Finding a column of integers and three numbers for 0 requires that the returned results be ordered sequentially and that the returned sequences differ from each other. For example:

Array S = {-1 0 1 2-1-4},
A Solution set is:
(-1, 0, 1) (-1,-1, 2)

Ideas

(It seems that I really do not have a bit of algorithmic mind)

For each current number, use the 2sum algorithm to find the remaining two numbers and the inverse number for the current number. 3sum degenerate into a 2sum problem.

Since the title does not require the return sequence ordinal, it can be used to sort the 2sum and then double-pointer traversal algorithm.

"Other Code"

vector<vector<int>> Threesum (vector<int>&num) {Vector<vector<int> >ret;        Ret.clear (); Sort (Num.begin (), Num.end ());//soga!!         for(intI=0; I!=num.size (); i++){            if(I >0&& num[i]==num[i-1])                Continue;//Duplicate triplets            intj,k; J=i+1; K=num.size ()-1;  while(j<k) {                if(j>i+1&&num[j]==num[j-1]) {J++; Continue; }                if(K<num.size ()-1&& num[k]==num[k+1]) {k--; Continue; }                 intsum = Num[i] + num[j] +Num[k]; if(sum>0) {k--; }Else if(sum<0) {J++; }Else{vector<int>tmp;                    Tmp.push_back (Num[i]);                    Tmp.push_back (Num[j]);                    Tmp.push_back (Num[k]);                    Ret.push_back (TMP); J++; }            }        }        returnret; } 

Summary

Because there are repeating numbers in the sequence, to remove duplicates, J and K are double-pointer searches.

It may be strange to do what happens if you omit the number of repetitions ( -1,-1,2).

So I debugged a bit by step = =

I point to-1, j=i+1, point to-1, so that you do not miss the three number of two numbers in the same situation. O (∩_∩) o~~

In addition, the STL's sort function should be a quick line, with the time O (Nlogn). The library function is very convenient, also thought to have to write manually ... Lazy cancer.

PostScript

Although the time spent a morning, but summed up the T1 2sum, read the beauty of Programming 2.12 section related explanations, or a little harvest.

OJ Practice 48--t15 3Sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.