[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 < ...
随机推荐
- 什么是ajax,ajax原理是什么 ,优缺点是什么
AJAX工作原理及其优缺点 1.什么是AJAX?AJAX全称为“Asynchronous JavaScript and XML”(异步JavaScript和XML),是一种创建交互式网页应用的网页 ...
- CADisplayLink
什么是CADisplayLink CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个r ...
- MITK Tutorial
MITK 设计来使用模块化和高度的代码重用,既能作为 pure software library或complete application framework.它的结构概览图如下: ITK:提供分 ...
- EntityFrame6在本地可以正常使用,部署到IIS后报异常(Additional information: The underlying provider failed on Open.)
异常详细:An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlS ...
- mvc 分页js
<script type="text/javascript"> var configA = { options: { m ...
- PHP之mysql_real_escape_string()函数讲解
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- Android串口通信(基于Tiny6410平台)
友善之臂的Android系统有他们自己编写的一个串口通信程序,网上没有找到他的源代码,而且界面操作不在一个界面,不是很方便,这里我自己写了一个粗糙点的串口通信程序. 同样这里还是调用友善之臂的frie ...
- POJ 1850
#include <iostream> #include <string> using namespace std; int fac(int num); int C(int n ...
- JsRender系列demo(4)-if else
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- [三分]HDOJ 5531 Rebuild
题意:给n个点,以这n个点为圆心画圆,使得所有的圆与其相邻的圆相切. 求n个圆最小的面积和. 分析:很容易想到确定了其中一个圆的半径之后,其他的圆的半径也能随之确定了. 画一画三个点的和四个点的,会发 ...