当前位置:网站首页>Leetcode-15 sum of three numbers
Leetcode-15 sum of three numbers
2022-06-11 00:03:00 【It was the sea】

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums.sort()
res, k = [], 0
for k in range(len(nums) - 2):
if nums[k] > 0: break # 1. because of j > i > k.
if k > 0 and nums[k] == nums[k - 1]: continue # 2. skip the same `nums[k]`.
i, j = k + 1, len(nums) - 1
while i < j: # 3. double pointer
s = nums[k] + nums[i] + nums[j]
if s < 0:
i += 1
while i < j and nums[i] == nums[i - 1]: i += 1
elif s > 0:
j -= 1
while i < j and nums[j] == nums[j + 1]: j -= 1
else:
res.append([nums[k], nums[i], nums[j]])
i += 1
j -= 1
while i < j and nums[i] == nums[i - 1]: i += 1
while i < j and nums[j] == nums[j + 1]: j -= 1
return res
边栏推荐
- 关于优化API接口响应速度
- LabVIEW uses the visa read function to read USB interrupt data
- Is the financial management of qiniu school reliable and safe
- 2022 college entrance examination quantitative paper | please answer the questions for quantitative candidates
- Redis installation and common problem solving based on centeros7 (explanation with pictures)
- [mathematics] [continuum mechanics] symmetry tensor, strain tensor and stress tensor in fluid mechanics
- [pyGame games] here it is. This Gobang game is super A. share it with your friends~
- flutter 如何去掉listview顶部空白的问题
- Top ten information security principles
- About optimizing API interface response speed
猜你喜欢

BGP - route map extension (explanation + configuration)

B 树的简单认识
![[pyGame] stir up your brain and play the](/img/0c/fd558c843705af19720d790da4ff06.jpg)
[pyGame] stir up your brain and play the "24 o'clock" idea together ~ (awesome)

【Pygame小游戏】这款“打地鼠”小游戏要火了(来来来)

LabVIEW使用MathScript Node或MATLAB脚本时出现错误1046

vtk.js中vtp下载

Easyrecovery15 simple and convenient data recovery tool

LabVIEW调用DLL时出现异常0xc0000005代码
![[turtle confessions collection]](/img/81/b4bacc23691e58e403f1330d0ca7cf.jpg)
[turtle confessions collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." Be happy for the rest of your life, and be safe for ever ~

【Pygame小游戏】首月破亿下载 一款高度融合了「超休闲游戏特性」的佳作~
随机推荐
30 | 怎么重设消费者组位移?
LabVIEW执行串行回送测试
LabVIEW锁相环(PLL)
[pyGame] this classic bomber super game is online. Do you love it? (source code attached)
LabVIEW open other exe programs
LabVIEW改变Point ROI Overlay的形状或颜色
Exception 0xc00000005 code occurred when LabVIEW called DLL
Select sort
[pyGame collection] please check the game guide through childhood: are there any games you have played? (attach five source codes for self access)
关于优化API接口响应速度
【Opencv实战】寒冷的冬季,也会迎来漫天彩虹,这特效你爱了嘛?
【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
The serial port in the visa test panel under LabVIEW or max does not work
[auto reply Script] happy new year. I typed every word myself, not forwarded it~
【Pygame合集】回忆杀-“童年游戏”,看看你中几枪?(附五款源码自取)
Baidu quick collection SEO optimization keyword ranking optimization skills
【Pygame小游戏】来了来了它来了——这款五子棋小游戏超A的,分享给你的小伙伴儿一起pk吧~
干货丨MapReduce的工作流程是怎样的?
About optimizing API interface response speed
How to handle the database query error with Emoji expression in Typecho- Xingze V Club