python三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
# 存储结果列表
res_list = []
# 对nums列表进行排序,无返回值,排序直接改变nums顺序
nums.sort()
for i in range(len(nums)):
# 如果排序后第一个数都大于0,则跳出循环,不可能有为0的三数之和
if nums[i] > 0:
break
# 排序后相邻两数如果相等,则跳出当前循环继续下一次循环,相同的数只需要计算一次
if i > 0 and nums[i] == nums[i-1]:
continue
# 记录i的下一个位置
j = i + 1
# 最后一个元素的位置
k = len(nums) - 1
while j < k:
# 判断三数之和是否为0
if nums[j] + nums[k] == -nums[i]:
# 把结果加入数组中
res_list.append([nums[i], nums[j], nums[k]])
# 判断j相邻元素是否相等,有的话跳过这个
while j < k and nums[j] == nums[j+1]: j += 1
# 判断后面k的相邻元素是否相等,是的话跳过
while j < k and nums[k] == nums[k-1]: k -= 1
# 没有相等则j+1,k-1,缩小范围
j += 1
k -= 1
# 小于-nums[i]的话还能往后取
elif nums[j] + nums[k] < -nums[i]:
j += 1
else:
k -= 1
return res_list if __name__ == '__main__':
s = Solution()
result_list = s.threeSum([-1, 0, 1, 2, -1, -4])
print(result_list)
python三数之和的更多相关文章
- lintcode: 三数之和II
题目 三数之和 II 给一个包含n个整数的数组S, 找到和与给定整数target最接近的三元组,返回这三个数的和. 样例 例如S = . 和最接近1的三元组是 -1 + 2 + 1 = 2. 注意 ...
- lintcode:三数之和
题目 三数之和 给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组. 样例 如S = {-1 0 1 2 -1 -4}, 你需要返回的三元组集 ...
- [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 < ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [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 ...
随机推荐
- asp.net 调用 WNetAddConnection2 window api 访问被拒绝
通过Asp.net 程序调用局域网另外一台机器文件,显示拒绝访问,单独编写控制台程序正常. 修改iis 应用程序池标识,为管理员用户即可!!
- Linux进程调度策略的发展和演变--Linux进程的管理与调度(十六)
1 前言 1.1 进程调度 内存中保存了对每个进程的唯一描述, 并通过若干结构与其他进程连接起来. 调度器面对的情形就是这样, 其任务是在程序之间共享CPU时间, 创造并行执行的错觉, 该任务分为两个 ...
- c/c++ 字节对齐
c 字节对齐 概念: 结构体里会包括各种类型的成员,比如int char long等等,它们要占用的空间不同,系统为一个结构体开辟内存空间时,会有2种选择. 第一种:节省空间的方案,以上面的列子来说的 ...
- shell 获取时间
获取当前时间 t=$(date +"%Y-%m-%d %H-%M-%S") echo $t 获取前一天的当前时间 time=$(date -d "-1 day" ...
- 《Java大学教程》—第21章 高级案例研究
21.3 需求:P510用例模型(use case model):用例图(use case diagram).用例(use case).行为说明(behaviour specification) ...
- (转)Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
http://www.ityouknow.com/springboot/2018/03/28/dockercompose-springboot-mysql-nginx.html 我知道大家这段时间看了 ...
- ES5-ES6-ES7_字符串扩展—模板字符串
includes(), startsWith(), endsWith() 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法 ...
- node基础—process对象(管理进程)
process对象概述 process对象是一个全局对象,可以在任何地方都能访问到他,通过这个对象提供的属性和方法,使我们可以对当前运行的程序的进程进行访问和控制 process 对象是一个 glob ...
- Thread.interrupt()
作者:Intopass链接:https://www.zhihu.com/question/41048032/answer/89431513来源:知乎著作权归作者所有.商业转载请联系作者获得授权 ...
- Linux之命令初识
Linux与windows目录结构对比 命令mkdir.ls.ls -l.cd .pwd [root@oldboyedu-01 ~]# #创建目录 make directory mkdir [root ...