# [Leetcode 15] 3sum

Source: Internet
Author: User

Algorithm slag. For details about the reality, refer to or completely copy [Dai fangqin ([email protected])] The leetcode solution of the great god. Here we only record the problem.

Early AC, now TLE

`Class solution {public: vector <int> threesum (vector <int> & num) {vector <int> result; If (Num. size () <3) return result; // The sort (Num. begin (), num. end (); // sort const int target = 0; Auto last = num. end (); For (Auto A = num. begin (); A <Prev (last, 2); ++ A) {auto B = next (a); Auto c = Prev (last ); int sum = * A + * B + * C; while (B <c) {If (sum <target) {+ + B;} else if (sum> target) {-- C;} else {result. push_back ({* a, * B, * c}); // note that ++ B; -- C ;}} sort (result. begin (), result. end (); result. erase (unique (result. begin (), result. end (), result. end (); return result ;}};`

Note:

1. Because the elements are repeated, it is obvious that the above problem cannot be solved by map.

2. function unique

Remove consecutive duplicates in range

Removes all but the first element from every consecutive group of equivalent elements in the range`[first,last)`.

The function cannot alter the properties of the object containing the range of elements (I. E ., it cannot alter the size of an array or a container): the removal is done by replacing the duplicate elements by the next element that is not a duplicate, and signaling the new size of the shortened range by returning an iterator to the element that should be considered its newPast-the-endElement.

3. member function erase

Removes from the vector either a single element (Position) Or a range of elements ([First, last)).
This variable tively has CES the container size by the number of elements removed, which are destroyed.
Because vectors use an array as their underlying storage, erasing elements in positions other than the vector end causes the container to relocate all the elements after the segment erased to their new positions. this is generally an inefficient operation compared to the one completed MED for the same operation by other kinds of sequence containers (such as list orforward_list ).

[Leetcode 15] 3sum

Related Keywords:

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.