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 ...
随机推荐
- 显示 Ubuntu 11.10 的 终端窗口
显示 Ubuntu 11.10 的 终端窗口 一.点击左上角的图标 -> 在search框里搜索termial . 二.快捷键:Ctrl+Alt+t.
- STL:字符串用法详解
字符串是程序设计中最复杂的变成内容之一.STL string类提供了强大的功能,使得许多繁琐的编程内容用简单的语句就可完成.string字符串类减少了C语言编程中三种最常见且最具破坏性的错误:超越数组 ...
- polaris: 一个用go实现的支持restful的web框架
介绍 polaris是一个用go实现的支持restful的web框架,主要参考tornado进行设计. 虽然在go里面搭建一个http server非常的简单,这里强烈推荐gorilla,但并没有很好 ...
- Cocos2D v2.0至v3.x简洁转换指南(一)
在该指南开头,我们假设你应经很熟悉Cocos2d 2.x版本. 我们将指出新版本重要的改变,并且给出一些你已经从Cocos2d 2.x版本中熟知的实现. CCNodes,CCScenes和CCLaye ...
- 【Android 应用开发】Android - 时间 日期相关组件
源码下载地址 : -- CSDN : http://download.csdn.net/detail/han1202012/6856737 -- GitHub : https://github.co ...
- 【面试笔试算法】Program 3 : Complicated Expression(网易游戏笔试题)
时间限制:50000ms 单点时限:5000ms 内存限制:256MB 描述 在lisp语言中,表达式都用前缀法表示,例如,1 + 2 在lisp中使用(+ 1 2)来表示,其中,表达式的括号是必需的 ...
- unity连接photon服务端模块
using UnityEngine; using System.Collections; using System; public class PhotonConnection : Photon.Mo ...
- PS图层混合算法之五(饱和度,色相,颜色,亮度)
饱和度模式: HcScYc =HBSAYB 饱和度模式:是采用底色的亮度.色相以及绘图色的饱和度来创建最终色.如果绘图色的饱和度为0,则原图没有变化. 输出图像的饱和度为上层,色调和亮度保持为下层. ...
- Oracle E-Business Suite Maintenance Guide Release 12.2(Patching Procedures)
更多内容参考: http://docs.oracle.com/cd/E51111_01/current/acrobat/122ebsmt.zip Preparing for Patching For ...
- "《算法导论》之‘队列’":队列的三种实现(静态数组、动态数组及指针)
本文有关栈的介绍部分参考自网站数据结构. 1. 队列 1.1 队列的定义 队列(Queue)是只允许在一端进行插入,而在另一端进行删除的运算受限的线性表. (1)允许删除的一端称为队头(Front) ...