We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fd6f832 commit cbbc73cCopy full SHA for cbbc73c
0020.有效的括号/0020-有效的括号.py
@@ -4,14 +4,13 @@ def isValid(self, s):
4
:type s: str
5
:rtype: bool
6
"""
7
- mapping = {")":"(", "]":"[", "}":"{"}
+ dic = {")": "(", "]":"[", "}":"{"}
8
stack = []
9
- for i, char in enumerate(s):
10
- if char not in mapping:#left
11
- stack.append(char)
+ for ch in s:
+ if ch in ["(", "[", "{"]:
+ stack.append(ch)
12
else:
13
- if not stack or stack[-1] != mapping[char]:
+ if not stack or dic[ch] != stack[-1]:
14
return False
15
stack.pop()
16
-
17
return len(stack) == 0
0 commit comments