class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i,item in enumerate(nums):
b=target-item
if b in nums:
j=nums.index(b)
if i!=j:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i,item in enumerate(nums):
b=target-item
if b in nums:
j=nums.index(b)
if i!=j:
return [i,j]
执行用时:1388 ms
本文介绍了一种解决'两数之和'问题的Python算法实现。通过遍历列表找到目标值对应的两个数及其索引位置。该算法适用于初学者理解如何使用Python的基本语法结构解决问题。
5147

被折叠的 条评论
为什么被折叠?



