網站首頁 編程語言 正文
三數之和題目描述
給你一個包含 n 個整數的數組 nums,判斷 nums 中是否存在三個元素 a,b,c ,
使得 a + b + c = 0 ?請你找出所有滿足條件且不重復的三元組。
答案中不允許包含重復的三元組。
示例:
給定數組 nums = [-1, 0, 1, 2, -1, -4],
滿足要求的三元組集合為:
[
? [-1, 0, 1],
? [-1, -1, 2]
]
思路
1. 首先將數組排序,可以利用Python內置函數,也可以利用另外定義排序算法。
2. 應用雙指針算法。固定第一個數,索引為i,遍歷整個數組,第一個數也是三個數中最小的數,然后在該數右面設置左右兩個指針l和r,l=i+1,r=len(nums)-1,
3. 判斷這三個索引指向的元素和與0的大小關系。
和>0,右指針左移一位;和<0,左指針右移一位。
由于要避免重復的三元組,所以移動左右指針的時候要跳過相鄰的所有相等的nums[i]。
Python3代碼
#導入計算時間的包,調用系統時間
from time import *
#初始時間
t1 = time()
def threeSum(nums):
nums.sort()
n = len(nums)
res = []
for i in range(n):
'''如果相鄰的兩個數相等,跳過,避免重復'''
if i > 0 and nums[i] == nums[i-1]:
continue
l, r = i+1, n-1
while l < r:
if nums[i] + nums[l] + nums[r]>0:
r -= 1
while nums[r+1] == nums[r]:
r -= 1
elif nums[i] + nums[l] + nums[r]<0:
l += 1
while nums[l-1] == nums[l]:
l += 1
else:
res.append([nums[i],nums[l],nums[r]])
l += 1
r -= 1
while nums[l] == nums[l - 1]: l += 1
while nums[r] == nums[r + 1]: r -= 1
return res
if __name__ == '__main__':
nums = [-1,0,1,2,-1,-4]
print(threeSum(nums))
#結束時間
t2 = time()
#運行時間
run_time = t2 - t1
print(run_time)
運行結果:
[[-1, -1, 2], [-1, 0, 1]]
#運行時間
0.0010113716125488281
以上代碼有一些思想錯誤:
遺漏了如果三個數全部大于0,則退出循環,因為沒有滿足條件的結果。
沒有嚴格判斷每一次的l<r的條件。
修正后的代碼:
from time import *
t1 = time()
def threeSum(nums):
nums.sort()
n = len(nums)
res = []
for i in range(n-2):
if nums[i] > 0:break
'''如果相鄰的兩個數相等,跳過,避免重復'''
if i > 0 and nums[i] == nums[i-1]:
continue
l, r = i+1, n-1
while l < r:
if nums[i] + nums[l] + nums[r]>0:
r -= 1
while l < r and nums[r-1] == nums[r]:
r -= 1
elif nums[i] + nums[l] + nums[r]<0:
l += 1
while l < r and nums[l] == nums[l-1]:
l += 1
else:
res.append([nums[i],nums[l],nums[r]])
l += 1
r -= 1
while l < r and nums[l] == nums[l - 1]: l += 1
while l < r and nums[r] == nums[r + 1]: r -= 1
return res
if __name__ == '__main__':
nums = [-2,-3,0,0,-2]
print(threeSum(nums))
t2 = time()
run_time = t2 - t1
print(run_time)
結果:
[]
#時間
0.0
原文鏈接:https://blog.csdn.net/weixin_43937698/article/details/110697815
相關推薦
- 2021-12-20 Win10配置Hadoop環境變量
- 2022-11-17 獲取C++變量類型的簡單方法_C 語言
- 2022-11-13 pandas進階教程之Dataframe的apply方法_python
- 2022-08-29 python中requests庫安裝與使用詳解_python
- 2022-11-14 mq消息積壓怎么對應
- 2022-08-06 詳解Python如何優雅地解析命令行_python
- 2023-03-01 Android使用AndroidUtilCode實現多語言_Android
- 2022-05-01 Python中的datetime包與time包包和模塊詳情_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支