|
发表于 2020-8-2 17:53:40
|
显示全部楼层
本帖最后由 Twilight6 于 2020-8-2 17:56 编辑
我这里正常运行,只是这里代码错了:for i in num in enumerate(nums):
- from typing import List
- class Solution():
- def twoSum(self, nums:List[int], target:int) -> List[int]:
- hashmap = {}
- for index , num in enumerate(nums):
- hashmap[num] = index
- for i in num in enumerate(nums):
- j = hashmap.get(target - num)
- if j is not None and i != j:
- return [i, j]
- s = Solution()
- s.twoSum([2, 7, 11, 15], 9)
复制代码
改成这样试试看:
- from typing import List
- class Solution():
- def twoSum(self, nums:List[int], target:int) -> List[int]:
- hashmap = {}
- for index , num in enumerate(nums):
- hashmap[num] = index
- for i,num in enumerate(nums):
- j = hashmap.get(target - num)
- if j is not None and i != j:
- return [i, j]
- s = Solution()
- print(s.twoSum([2, 7, 11, 15], 9))
复制代码
返回:
|
|