[Lintcode 3sum] Three number of the sum (Python, two points)

Source: Internet
Author: User
Tags lintcode

Title Link: http://www.lintcode.com/zh-cn/problem/3sum/?rand=true#

Use this OJ to practice python ... This test instructions and solution is not much to say, O (N^2LGN) on the line, the key is!! Python syntax ...

To order the tuple, if the direct sort will automatically turn to list, this time to turn back.

1 classSolution:2     """3 @param numbersbers:give An array numbersbers of n integers4 @return: Find all unique triplets in the array which gives the sum of zero.5     """6     defsolution (self):7         Pass8     defthreesum (self, Numbers):9         #Write your code hereTen Numbers.sort () OneRET = [] An =len (Numbers) -          forIinchRange (0, N): -              forJinchRange (i+1, N): theD =-(Numbers[j] +Numbers[i]) -L =0 -r = n-1 -p =-1 +                  whileL <=r: -m = (L + r) >> 1 +                     ifNUMBERS[M] = =D: Ap =m at                          Break -                     elifNUMBERS[M] >D: -r = M-1 -                     elifNUMBERS[M] <D: -L = m + 1 -                 ifP! =-1 andP! = I andP! =J: in Ret.append ((Numbers[i], numbers[j], numbers[p])) -nn =Len (ret) to          forIinchRange (0, nn): +Ret[i] =list (Ret[i]) - Ret[i].sort () theRet[i] =tuple (ret[i]) *RET =List (set (ret)) $         returnRet

[Lintcode 3sum] Three number of the sum (Python, two points)

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.