[LeetCode][Python]15:3Sum
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 15: 3Sum
https://oj.leetcode.com/problems/3sum/ Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0?
Find all unique triplets in the array which gives the sum of zero. Note:
Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:
(-1, 0, 1)
(-1, -1, 2) ===Comments by Dabay===
先排序,然后从左到右固定一个数,在后边的数列中使用左右指针往中间靠拢的方法查找。 从左往右固定一个数(如果需要判断的数与之前的相等就跳过),
左右两个指针往中间靠拢的时候,能够保证不遗漏。(如果找到一组数据之后,左指针继续移动到下一个数不相等的数)
'''
class Solution:
# @return a list of lists of length 3, [[val1,val2,val3]]
def threeSum(self, num):
if len(num) < 3:
return []
num.sort()
res = []
for i in xrange(len(num)-2):
if i>0 and num[i]==num[i-1]:
continue
target = 0 - num[i]
j, k = i+1, len(num)-1
while j < k:
if num[j] + num[k] == target:
res.append([num[i], num[j], num[k]])
j = j + 1
while j<k and num[j]==num[j-1]:
j = j + 1
elif num[j] + num[k] < target:
j = j + 1
else:
k = k - 1
return res def main():
sol = Solution()
nums = [0,0,0,0,0]
solutions = sol.threeSum(nums)
for solution in solutions:
print solution if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]15:3Sum的更多相关文章
- LeetCode(15)3Sum
题目如下: Python代码: def threeSum(self, nums): res = [] nums.sort() for i in xrange(len(nums)-2): if i &g ...
- [Leetcode][Python]44:Wildcard Matching
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 44:Wildcard Matchinghttps://oj.leetcode ...
- leetcode第15题--3Sum
Problem: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...
- 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 ...
- [LeetCode]题解(python):002-Add Two Numbers
题目来源: https://leetcode.com/problems/add-two-numbers/ 题意分析: 这道题目是要将两个单链条相加.输出得到的新链条. 题目思路: 不难发现,其实题目就 ...
- [LeetCode]题解(python):016-3Sum Closest
题目来源: https://leetcode.com/problems/3sum-closest/ 题意分析: 这道题目输入一个数组nums和一个数target,找出数组中三个数,使得他们的和最接近t ...
- [LeetCode]题解(python):015-3Sum
题目来源: https://leetcode.com/problems/3sum/ 题意分析: 这道题目是输入一个数组nums.找出所有的3个数使得这3个数之和为0.要求1.输出的3个数按小到大排序, ...
- [LeetCode]题解(python):018-4Sum
题目来源: https://leetcode.com/problems/4sum/ 题意分析: 这道题目和3Sum的题目类似,找出所有的4个数,使得这4个数等于target. 题目思路: 这道题做法和 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
随机推荐
- gridView行号的显示
我们在进行开发的时候,很多地方希望dataGridview或girdView显示行号,这里我来说一下两种的实现方法 在girdView中很简单很好实现,我在这里写一下代码,具体其他功能可以看其带的DE ...
- javascript 数据结构和算法读书笔记 > 第三章 列表
1. 结构分析 列表首先要有以下几个属性: listSize 长度 pos 当前位置 dataStore 数据 我们要通过以下方法对上面三个属性进行操作: length() 获取长度 | getPos ...
- 使用Python多线程犯的错误总结
在使用Python多线程的时候,在使用多线程编程的时候,由于对于变量作用域和多线程不是很熟悉,导致在使用多线程的时候,犯了低级的错误. 第一个错误: 在多线程中使用全局变量,导致多个线程修改全局变量. ...
- jchat:linux聊天程序2:MySQL
该软件使用的数据库为MySQL,因为它免费.开源,在linux下几乎就是最好的选择. 首先要在mysql中root用户新建数据库并赋权给本用户: create database jchat; gran ...
- C语言基础04
什么叫数组呢?我们的第一反应是很多数组合在一起就是数组,字面意思,当然不仅仅是数字,字符也是可以的. 数组属于构造类型 .指相同类型的若干变量组织起来. 类型说明符 数组名 [常量表达式] int ...
- MVC 创建带图片的<A></A>标签
<a href="@Url.Action("Detail", "Product", new { messageId = item.message ...
- DenyHosts安装及配置
一.DenyHost简介 DenyHosts是Python语言写的一个程序软件,运行于Linux上预防SSH暴力破解的,它会分析sshd的日志文件(/var/log/secure),当发现重复的攻击时 ...
- @Transactional 注解说明
先让我们看代码吧! 以下代码为在"Spring3事务管理--基于tx/aop命名空间的配置"基础上修改.首先修改applicationContext.xml如下: <pre ...
- ubuntu系统下设置静态IP
改动 /etc/network/interfaces文件 加入下面内容 # The loopback network interface auto lo eth0 iface lo inet loop ...
- C语言中的回调函数
C语言中通过函数指针实现回调函数(Callback Function) ====== 首先使用typedef定义回调函数类型 ====== typedef void (*event_cb_t)(co ...