TwoSum

发布时间:2026-02-17 17:35

最新推荐文章于 2025-08-12 12:48:44 发布

原创 于 2019-05-06 02:25:25 发布 · 153 阅读

· 0

· 0 ·

CC 4.0 BY-SA版权

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

"""复杂度O(n)"""

class Solution:

def twoSum(self, nums, target):

dict1={}

for i in range(len(nums)):

if nums[i] in dict1:

return [dict1[nums[i]],i]

else:

dict1[target-nums[i]] = i

if __name__ =="__main__":

s=Solution()

s.twoSum([2,7,11,15],9)

python

运行

"""双指针"""

class Solution:

def twoSum(self,nums,target):

l=len(nums)

array = list(zip(nums,range(l)))

array.sort()

low=0

high=l-1

while low<high:

S=array[low][0]+array[high][0]

if S<target:

low += 1

elif S>target:

high -= 1

else:

return [array[low][1],array[high][1]]

python

运行

网址:TwoSum https://www.yuejiaxmz.com/news/view/1442667

相关内容

幸运数探秘
【大模型】提示词工程——12个提示词技巧整理

随便看看