我是leetcode的新手。所以我一直在解决一些问题,我遇到了“旋转数组”这个问题。这是链接:https://leetcode.com/problems/rotate-array/?envType=study-plan-v2&id=top-interview-150
这是我的解决方案代码(Python 3):
class Solution:
def rotate(self, nums: List[int], k: int) -> None:
for _ in range (k):
nums = nums[-1:] + nums[:-1]
solution = Solution()
solution.rotate([-1, -100, 3, 99], 2)
我已经在 Pycharm 中测试了代码,它给了我应该有的结果,但是当我在这里运行代码时,它说输出是错误的。有人可以帮助我吗?
谢谢。
根据幕后的 python 版本和导入,这一点:
nums: List[int]
可能会给您带来错误。特别是 List
大写 L
。那必须进口。
将
from typing import List
放在文件的顶部。或者,将其从 nums: List[int]
更改为 nums: list[int]
.