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)