题目链接:http://www.lintcode.com/zh-cn/problem/3sum/?rand=true#

用这个OJ练练python…这个题意和解法就不多说了,O(n^2lgn)就行了,关键是!!python的语法…

要想给tuple排序,如果直接sort的话会自动转成list,这个时候要再转回来。

 class Solution:
"""
@param numbersbers : Give an array numbersbers of n integer
@return : Find all unique triplets in the array which gives the sum of zero.
"""
def Solution(self):
pass
def threeSum(self, numbers):
# write your code here
numbers.sort()
ret = []
n = len(numbers)
for i in range(0, n):
for j in range(i+1, n):
d = -(numbers[j] + numbers[i])
l = 0
r = n - 1
p = -1
while l <= r:
m = (l + r) >> 1
if numbers[m] == d:
p = m
break
elif numbers[m] > d:
r = m - 1
elif numbers[m] < d:
l = m + 1
if p != -1 and p != i and p != j:
ret.append((numbers[i], numbers[j], numbers[p]))
nn = len(ret)
for i in range(0, nn):
ret[i] = list(ret[i])
ret[i].sort()
ret[i] = tuple(ret[i])
ret = list(set(ret))
return ret

[Lintcode 3sum]三数之和(python,二分)的更多相关文章

  1. [LintCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  2. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  3. lintcode:三数之和

    题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...

  4. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  5. [LeetCode] 15. 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  6. [leetcode]15. 3Sum三数之和

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  7. 【LeetCode每天一题】3Sum(三数之和)

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  8. Leetcode15.3Sum三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  9. [LeetCode] 259. 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

随机推荐

  1. Android开发在使用第三方推送的时候出现INSTALL_FAILED_VERSION_DOWNGRADE

    [-- :: - push_getui_test] Uploading push_getui_test.apk onto device 'emulator-5554' [-- :: - push_ge ...

  2. [搜片神器]服务器SQL2005查询分页语句你理解了么

    在sosobt.com网站准备采用Lucence.net来进行索引处理搜索慢问题的时候,突然发现常用的分页获取数据的row_number也支持不住了,后期查到200多万的时候非常慢(总数据有500万) ...

  3. dragsort拖动插件的使用

    <!DOCTYPE html><html><head> <title>DragSort Example</title> <meta c ...

  4. HtmlAgilityPack 简单运用

    WebClient client = new WebClient(); MemoryStream ms = new MemoryStream(client.DownloadData("htt ...

  5. DX SetFVF

    自由顶点格式(flexible vertex format,FVF) http://www.cnblogs.com/xmzyl/articles/1604096.html if( SUCCEEDED( ...

  6. [设计模式] 11 享元模式 Flyweight

    转 http://blog.csdn.net/wuzhekai1985/article/details/6670298 问题 在面向对象系统的设计何实现中,创建对象是最为常见的操作.这里面就有一个问题 ...

  7. fedora下缺少autopoint包的解决办法

    编译过程中,报错,缺少autopoint包 然而无论是yum install autopoint 还是yum search autopoint都没有理想的答案 执行yum install gettex ...

  8. jsp中文件下载的实现

    jsp中实现文件下载的最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>.但是这样服务器上的目录资源会直 ...

  9. [shell编程]正则表达式

    如果在shell脚本中处理数据文件,那么我们就必须熟悉正则表达式.正则表达式是用来过滤数据流中文本的模式模板,模式由标准文本字符和特殊字符组成.正则表达式用特殊字符来匹配一系列一个或多个字符,要想掌握 ...

  10. java基础知识回顾之javaIO类--File类

    File类是对文件系统中文件以及目录(文件夹)进行封装的对象,可以通过面向对象的思想来操作文件和目录(文件夹).File类保存文件或目录的各种元素的信息,包括文件名,文件长度,最后修改日期,是否可读, ...