力扣 ——3Sum python (三数之和)实现
题目描述:
中文:
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
英文:
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
if len(nums) < 3:
return result
nums.sort()
for i in range(0, len(nums)):
j = i + 1
k = len(nums) - 1
if i > 0 and nums[i] == nums[i - 1]:
continue #
while j < k:
sum = nums[i] + nums[j] + nums[k]
if sum == 0:
result.append((nums[i], nums[j], nums[k]))
k -= 1
j += 1
while(nums[j] == nums[j - 1] and nums[k] == nums[k + 1] and j < k):
j += 1
elif sum > 0:
k -= 1
while(nums[k] == nums[k + 1] and j < k):
k -= 1
else:
j += 1
while(nums[j] == nums[j - 1] and j < k):
j += 1
return list(set(result))
题目来源:力扣
力扣 ——3Sum python (三数之和)实现的更多相关文章
- [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] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- python三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 力扣 ——4Sum (四数之和)python 实现
题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...
- 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 ...
- LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c ...
- 15. 3Sum[M]三数之和
题目 Given an array nums of n integers, are three elements a, b, c in nums such that a+b+c=0? Find all ...
- 3sum 求三数之和等于0,不允许重复
https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...
- 259 [LeetCode] 3Sum Smaller 三数之和较小值
题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...
随机推荐
- 为什么单个binlog会大于max_binlog_size设置
查看参数设置mysql> show global variables like '%max_binlog_size%';+-----------------+------------+| Var ...
- INSTR代替NOT LIKE
instr(title,'手册')>0 相当于 title like '%手册%' instr(title,'手册')=1 相当于 title like '手册%' instr(titl ...
- php chunk_split()函数 语法
php chunk_split()函数 语法 作用:把字符串分割为一连串更小的部分.东莞大理石平板 语法:chunk_split(string,length,end) 参数: 参数 描述 string ...
- HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5
Rikka with Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- LUOGU P4783 【模板】矩阵求逆(高斯消元)
传送门 解题思路 用高斯消元对矩阵求逆,设\(A*B=C\),\(C\)为单位矩阵,则\(B\)为\(A\)的逆矩阵.做法是把\(B\)先设成单位矩阵,然后对\(A\)做高斯消元的过程,对\(B\)进 ...
- 如何稀释 流事件 (如,onscroll、change、input、mouseover 等 事件)
1.问题引入:https://segmentfault.com/q/1010000000707337?_ea=62905 2.javascript中的函数节流和函数去抖:https://www.cnb ...
- vue搭配的UI框架 pc端 + 移动端
PC桌面端UI框架: 1,iview (最新,用户评分高功能多炫酷 解决和避免了其他UI框架出现的一些小问题) 2, bootstrap (使用用户最多样式死板没特色) 3,Element ...
- [CSP-S模拟测试]:蔬菜(二维莫队)
题目描述 小$C$在家中开垦了一块菜地,可以抽象成一个$r\times c$大小的矩形区域,菜地的每个位置都种着一种蔬菜.秋天到了,小$C$家的菜地丰收了. 小$C$拟定了$q$种采摘蔬菜的计划,计划 ...
- ubuntu16.04 常用软件
解决安装包依赖问题 更新数据库源 sudo apt-get update sudo apt-get -f -y install guake Terminal 作用:就是一个终端,按F12就出现,再按就 ...
- Visual Studio Code - 调试 Node.js 代码
官方的文档写的太好了!大家还是看参考资料吧. 参考资料: Debugging in Visual Studio Code Debug Node.js Apps using Visual Studio ...