[Lintcode 3sum]三数之和(python,二分)
题目链接: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,二分)的更多相关文章
- [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 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -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 ...
- [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 ...
- [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 ...
- 【LeetCode每天一题】3Sum(三数之和)
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...
- Leetcode15.3Sum三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- [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 < ...
随机推荐
- Spring中后台字符串国际化
1.在工程的资源文件夹(source folder)中建立三个properties文件:messages.properties(默认).messages_zh_CN.properties(中文).me ...
- 设置NODE_ENV=production
NodeJS - Express 4.0下设置环境变量NODE_ENV=production,并不是修改文件的配置信息,而是通过命令行来实现. 首先在命令行下进入项目的目录,然后先后执行如下命令: s ...
- System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本
问题描述: c#程序本机运行没有问题,部署到正式环境后报错: System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或更高版本 思考: 难道真是客户端的问题吗? ...
- c++ 格式化printf
类型为uint64_t的变量,使用printf进行打印时,需要区分操作系统: 64位系统:使用%ld 32位系统:使用%llu #include<stdio.h>#include < ...
- 【最小生成树】BZOJ 1196: [HNOI2006]公路修建问题
1196: [HNOI2006]公路修建问题 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1435 Solved: 810[Submit][Sta ...
- 【锋利的JQuery-学习笔记】遮罩层
效果图: 鼠标移动到上面后---> html: <div id="jnBrandList"> <ul> <li> <a href= ...
- RAD项目结构
- mybatis 插入日期类型精确到秒的有关问题
mybatis 插入日期类型精确到秒的问题 Mybatis 插入 数据库是为了防止插入空时报错, Mybatis 提供了一套机制,只要给定插入的字段的类型,如果为空,则它会自动处理为相应类型的默认值: ...
- C#画图
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- hdu 2486/2580 / poj 3922 A simple stone game 博弈论
思路: 这就是K倍动态减法游戏,可以参考曹钦翔从“k倍动态减法游戏”出发探究一类组合游戏问题的论文. 首先k=1的时候,必败态是2^i,因为我们把数二进制分解后,拿掉最后一个1,那么会导致对方永远也取 ...