这段代码为什么会报错:class Solution: def judge(self, alpha): words = "aeiou" if alpha in words: return True else: return False def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]: length = len(words) prefix = [0 for i in range(0, length + 1)] for i in range(1, length + 1): if judge(words[i-1][0]) and judge(words[i-1][-1]): prefix[i] = prefix[i-1] + 1 else: prefix[i] = prefix[i-1] ans = [] for query in queries: ans.append(prefix[query[1]+1] - prefix[query[0]]) return ans
时间: 2024-02-14 20:07:40 浏览: 74
1085(Triangle-War).zip_site:www.pudn.com_triangle_博弈_博弈论
这段代码会报错是因为在函数`vowelStrings`的循环中调用了函数`judge`,但是函数前面没有加上`self`,因此Python无法识别这个函数。需要将函数调用改为`self.judge`。
修改后的代码如下:
```
class Solution:
def judge(self, alpha):
words = "aeiou"
if alpha in words:
return True
else:
return False
def vowelStrings(self, words: List[str], queries: List[List[int]]) -> List[int]:
length = len(words)
prefix = [0 for i in range(0, length + 1)]
for i in range(1, length + 1):
if self.judge(words[i-1][0]) and self.judge(words[i-1][-1]):
prefix[i] = prefix[i-1] + 1
else:
prefix[i] = prefix[i-1]
ans = []
for query in queries:
ans.append(prefix[query[1]+1] - prefix[query[0]])
return ans
```
现在代码已经修改好了,可以正常运行。
阅读全文