[C + +] Leetcode:70 3Sum

Source: Internet
Author: User

Topic:

Given an array S of n integers, is 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:

    • Elements in a triplet (a,b,C) must is in non-descending order. (ie, a ≤ b ≤ c)
    • 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)

Ideas:

The solution is similar to the leetcode:15 3Sum Closest (plus JAVA Code). It is also the use of the first sort, after the clamping method to solve the answer.

The difference is that we need to constantly determine the new target, the Twosum, based on the first element's movement (change).

At the same time we need to consider how to skip the repeated collection, moving the clamping force of the two left and right pointers, we need to determine whether the elements are equal before and after the move, repeat skip.

Attention:

1. In order not to change the original array, I put the array in Ivec, then sort and follow.

Do not want to change the original array, so there is Ivec in        vector<int> Ivec (Num.begin (), Num.end ());                if (Num.size () < 3) return ret;                Sort (Ivec.begin (), Ivec.end ());
2. Outside the large cycle, we do not set the i++, in the body of the loop to judge, if there are repeated numbers, constantly skipped.

for (int i = 0; i < ivec.size ()-2;)

If there are duplicates, skip            do{                i++;            } while (I < ivec.size ()-1 && ivec[i-1] = = Ivec[i]);
3. After the loop, each time you need to calculate the opposite number of num[i], as target to match. There may be multiple matching cases, so even if a match succeeds, you still need to change the clamp pointer J and K to look for other possible matching combinations.

int twosum = 0-ivec[i]; -num[i] as target to find a matching two number            while (J < k) {//Even if the first matching item is found, still need to move the coordinates, looking for other possible matches   if (Ivec[j] + ivec[k] = = twosum)   {     vector<int> tmp{ivec[i], ivec[j], ivec[k]};     Ret.push_back (TMP);     do{         j + +;     } while (J < k && Ivec[j] = = ivec[j-1]);     do{          k--;     } while (J < k && Ivec[k] = = ivec[k+1]);    }
Complexity: O (n^2)

AC Code:

Class Solution {public:vector<vector<int> > Threesum (vector<int> &num) {Vector<vector        <int>> ret;                Do not want to change the original array, so there is Ivec in vector<int> Ivec (Num.begin (), Num.end ());                if (Num.size () < 3) return RET;                Sort (Ivec.begin (), Ivec.end ());        for (int i = 0; i < ivec.size ()-2;)            {Int J = i + 1;            int k = Ivec.size ()-1; int twosum = 0-ivec[i]; Use-num[i] as target to find a matching two number while (J < k) {//Even if the first match is found, you still need to move the coordinates to find other Possible match number if (Ivec[j] + ivec[k] = = twosum) {vector<int> tmp{ivec[i], IV                    EC[J], ivec[k]};                    Ret.push_back (TMP);                    do{J + +;                    }while (J < k && Ivec[j] = = ivec[j-1]);                    do{k--; }while (J < K &&                Ivec[k] = = ivec[k+1]);                } else if (Ivec[j] + ivec[k] < Twosum) J + +;            else k--;            }//If there are duplicates, skip the do{i++;        }while (I < Ivec.size ()-1 && ivec[i-1] = = Ivec[i]);    } return ret; }};

In fact, this problem can also be nested a twosum subroutine to do. The other blog posts are given. 3Sum

The point is, we must pay attention to the details of the handling, is the clamping force of the movement of the pointer.

[C + +] Leetcode:70 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.