题目链接: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. hash --C++

    题目来源:code[VS] 这是一个极其无聊的hash题.... 1230 元素查找 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数,询问该整数是否在n个正整数中出 ...

  2. LintCode-Majority Number

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  3. Java 8 VM GC Tunning Guide Charter 5

    第5章 Available GC The Java HotSpot VM includes three different types of collectors, each with differe ...

  4. HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢?例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?

    // test02.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...

  5. 【BZOJ】【3158】千钧一发

    网络流/最小割 这题跟BZOJ 3275限制条件是一样的= =所以可以用相同的方法去做……只要把边的容量从a[i]改成b[i]就行了- (果然不加当前弧优化要略快一点) /************** ...

  6. 新建android项目src和layout文件夹中没有内容的问题

    这个问题好像是最新版ADT(ver:23.0.0)才会出现的问题,解决办法也简单,直接把android SDK和ADT的老版本(ver:22.6.2)覆盖安装一次就好了.至于新版为什么会这么设计,现在 ...

  7. linux故障救援

    今天手贱,误操作将/usr/lib整个文件夹都删了.但是系统内还有很多重要的文件和数据,不敢贸然重装系统. 弄了个u盘启动. 一开始整个系统只有一个分区,原系统装在这个分区里,我用u盘启动后,用分区管 ...

  8. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

  9. Spring 与 Hibernate 集成 Transactional设置为只读

    @Transactional标签用于标记ServiceImpl使用事务,并且能够打开一个sessionFactory的session,并且打开事务. 如果在这个标签为@Transactional(pr ...

  10. 学习selenium需要具备的知识或技术

    因为有QTP和JAVA的基础,selenium学起来会快很多,总结下学习selenium所需要具备的知识或技术 1.selenium进行的自动化测试是基于ui层面的,所以html,css,javasc ...