我的问题是List定义为允许它在参数中使用而实际上被忽略。
在LeetCode Python3上,这将返回grid
class Solution:
def uniquePathsIII(self, grid: List[List[int]]) -> int:
return gird
但在其他Python3环境中,它将返回
Traceback (most recent call last):
File "main.py", line 1, in <module>
class Solution:
File "main.py", line 2, in Solution
def uniquePathsIII(self, grid: List[List[int]]) -> int:
NameError: name 'List' is not defined
我想在另一个环境中测试LeetCode的代码,我认为这几乎等同于LeetCode测试。
class Solution:
def uniquePathsIII(self, grid: [[int]]) -> int:
return grid
fin = Solution()
print(fin.uniquePathsIII([[1,0,0,0],[0,0,0,0],[0,0,2,-1]]))
但是我仍然想知道为什么List在LeetCode
的参数中是允许的在您的代码示例中,您写:
def uniquePathsIII(self, grid: [[int]]) -> int:
return grid
但是,这是不正确的语法。在最新版本的python中,类型注释是这样使用的:
def uniquePathsIII(self, grid: list[list[int]]) -> int:
return grid
要了解更多信息,我建议观看这个视频:https://www.youtube.com/watch?v=SMXsIX3PZ5w
同时,请确保您使用的是最新版本的Python (3.9+)