请问为什么我的列表下标明明能找到数据还会提示我列表下标越界?(输入的字符串s为"[]")
class Solution:
def isValid(self , s: str) -> bool:
# write code here
n = len(s)
if n%2 != 0:
return False
ls = []
for a in range(n):
while a<n and (s[a] == '(' or s[a] == '[' or s[a] == '{'):
ls.append(s[a])
a += 1
if s[a] == ')' and ls[-1] == '(':
ls.pop()
elif s[a] == ']' and ls[-1] == '[':
ls.pop()
elif s[a] == '}' and ls[-1] == '{':
ls.pop()
if not ls:
return True
else :
return False
程序异常退出, 请检查代码"是否有数组越界等异常"或者"是否有语法错误"
File "/tmp/a.py3", line 16, in run
ret = solution.isValid( s )
File "/tmp/solution.py", line 21, in isValid
elif s[a] == ']' and ls[-1] == '[':
IndexError: list index out of range
您可以用print在函数中打印信息分析问题