我已经开始学习Python,我想通过解决Leetcode问题来实践它。 我选择这个开头:https://leetcode.com/problems/two-sum/
在编辑器中,我选择了Python3,它提供了以下起始代码:
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
我已将此代码复制到Visual Studio代码中,并使用一些命令完成了此代码,但当我尝试运行它时,出现以下错误:
Exception has occurred: NameError
name 'List' is not defined
File "F:\Development\TestConsolePy\solution.py", line 2, in Solution
def twoSum(self, nums: List[int], target: int) -> List[int]:
File "F:\Development\TestConsolePy\solution.py", line 1, in <module>
class Solution:
我的Windows计算机上安装了Python 3.8.5
从我在网上看到的情况来看,python通常没有类型声明——但在我的例子中,我想我在选择python3并在我的机器上运行python3时肯定遗漏了一些东西。。。(如果我使用相同的命令并将它们粘贴到Leetcode生成的python代码中,那么它就不起作用了)
我误解了什么
from typing import List
应该能解决你的问题此模块还包含用于注释函数签名的其他对象,您可能希望导入这些函数签名以使其他潜在代码正常工作
相关问题 更多 >
编程相关推荐