threesum
算法题
问题描述:
在一些给定的数中,找到三个数,他们相加的和是0,并且这三个数的组合是不能重复的
例子:
input
[-1, 0, -1, 2, 1]
Output
[[-1, 1 ,0], [-1, -1, 2]]
解法:
Python代码:
Class threesum(object):
def sumthree(self, nums):
nums.sort() #先对nums数组进行排序,这里的nums是一个list变量
Numlist = []
for i in range(len(nums) -2):
if i !=0 and nums[i] == nums[i-1]:
Continue
J,k = i+1, len(nums)-1
While j <k:
S = nums[i] +nums[j] + nums[k]
If s>0 or (k < len(nums) -1 and nums[k ] == nums[k+1])
K -= 1
Elif s<0 or (k>i +1 and nums[j] == nums[j -1])
J -= 1
Else:
Numliist.append([nums[i], nums[j], nums[k]])
J += 1
K += 1
Return numlist
threesum的更多相关文章
- threeSum问题
三数之和等于0的问题: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- 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 ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [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分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 3sum问题的解决
其实一开始想错了,把这个问题想难了,导致没有思路,现在好了很多. 题目: Given an array S of n integers, are there elements a, b, c in S ...
- 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 ...
- python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
随机推荐
- python字符串与数字类型转化
数字转字符串:str(数字),如str(10) 相反:int(字符串),如int('10') 另外,import string后 用string.atoi('100',base),转换为int,bas ...
- EBS Concurrent Manager(并发管理器)异常处理[final]
来自:http://blog.itpub.net/35489/viewspace-742191/ 有时候我们在通过 adstpall.sh 关闭应用后,然后再使用adstrtal.sh开启.发现并发 ...
- 【Unity Shaders】Diffuse Shading——创建一个自定义的diffuse lighting model(漫反射光照模型)
本系列主要参考<Unity Shaders and Effects Cookbook>一书(感谢原书作者),同时会加上一点个人理解或拓展. 这里是本书所有的插图.这里是本书所需的代码和资源 ...
- close()方法应该在finally语句中调用吗?
翻译人员: 铁锚 翻译时间: 2013年12月20日 原文链接: Should .close() be put in finally block or not? 下面列出了关闭输出流(output w ...
- C语言中sizeof与strlen区别
本文转载自:http://www.2cto.com/kf/201109/105100.html 1. 以字符串形式出现的,编译器都会为该字符串自动添加一个0作为结束符,如在代码中写"abc& ...
- [转]高级SQL注入:混淆和绕过
############# [0×00] – 简介[0×01] – 过滤规避(Mysql)[0x01a] – 绕过函数和关键词的过滤[0x01b] – 绕过正则表达式过滤[0×02] – 常见绕过技术 ...
- 【Qt编程】Qt学习之窗口间的相互切换
在用Qt设计GUI时,经常要设计两个窗口之间的相互切换,即可以从一个窗口跳转到另一个窗口,然后又从另一个窗口跳转回原窗口.下面我们来介绍具体的实现方法: 工程建立及功能描述: 首先,我们建立Qt G ...
- iOS实现时间线列表效果(例如订单详情页面的效果)
之前看到美团的订单详情页面很有特色,于是决定模仿一下这个效果. 其实就是简单的 TableView 技巧,下面我们就来一步一步实现它. 设计 TableViewCell 原型 子类化一个新的 UITa ...
- ITU-T Technical Paper: 测量QoS的基本网络模型
本文翻译自ITU-T的Technical Paper:<How to increase QoS/QoE of IP-based platform(s) to regionally agreed ...
- 通过COM组件方式实现java调用C#写的DLL文件
转自这里 最近一段时间单位在做一个Web项目,工程师用JAVA语言,需要公用人员信息,统一用户名和密码,原有的平台中是用C#语言开发的,在网上查找解决方法,通过JAVA调用C#的DLL文件实现.网上资 ...