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 ...
随机推荐
- awk 循环语句例子
awk 循环语句例子 运行结果:
- 使用DBMS_LOCK控制程序并发
在调用并发的程序时,假设两个人同时提交了某一个并发请求,并且传入了同样的参数,假设在程序中使用了对状态的控制,例如在刚进入main函数的时候马上就将状态update了,那么其中某一个人就会查不到该条数 ...
- Java的字符串分割的不同实现
在java中实现字符串的分割相对而言是很简单的.我们一般会采取两中方式.一个是从jdk1.1就开始的StringTokenizer类,另一个是调用split方法进行分割.下面请看代码: import ...
- ISLR系列:(1)线性回归 Linear Regression
Linear Regression 此博文是 An Introduction to Statistical Learning with Applications in R 的系列读书笔记,作为本 ...
- 网站开发进阶(二十五)js如何将html表格导出为excel文件
js如何将html表格导出为excel文件 赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,共勉! jsp页面数据导出成excel的方法很 ...
- React Native之携程Moles框架
因为支持用javascript开发原生应用,React Native一推出就受到不少公司热捧,各家都跃跃欲试.但有一个痛点是,在移动端,我们是否有必要开发多套程序:iOS.Android和H5?本次将 ...
- 使用 /sys 文件系统访问 Linux 内核
sysfs 与 /sys sysfs 文件系统总是被挂载在 /sys 挂载点上.虽然在较早期的2.6内核系统上并没有规定 sysfs 的标准挂载位置,可以把 sysfs 挂载在任何位置,但较近的2.6 ...
- Material Design之CardView的使用
本文介绍CardView这个控件的使用,CardView继承至FrameLayout类,是support-v7包下的一个类,使用时必须引入cardview依赖包,可在下载的sdk文件夹中找到... 使 ...
- ffdshow 源代码分析 5: 位图覆盖滤镜(总结)
===================================================== ffdshow源代码分析系列文章列表: ffdshow 源代码分析 1: 整体结构 ffds ...
- Mahout Bayes分类
Mahout Bayes分类器是按照<Tackling the Poor Assumptions of Naive Bayes Text Classiers>论文写出来了,具体查看论文 实 ...