这段代码为什么会报错:class Solution: def judge(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 21:07:40 浏览: 76
这段代码会报错是因为在类`Solution`中的函数`judge`没有加上`self`参数,同时也缺少了一个冒号。
修改后的代码如下:
```
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
```
现在类`Solution`中的函数`judge`已经添加了`self`参数,并且语法也更加规范了。
阅读全文