Skip to content

Commit cbbc73c

Browse files
committed
2020-10-04
1 parent fd6f832 commit cbbc73c

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

0020.有效的括号/0020-有效的括号.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ def isValid(self, s):
44
:type s: str
55
:rtype: bool
66
"""
7-
mapping = {")":"(", "]":"[", "}":"{"}
7+
dic = {")": "(", "]":"[", "}":"{"}
88
stack = []
9-
for i, char in enumerate(s):
10-
if char not in mapping:#left
11-
stack.append(char)
9+
for ch in s:
10+
if ch in ["(", "[", "{"]:
11+
stack.append(ch)
1212
else:
13-
if not stack or stack[-1] != mapping[char]:
13+
if not stack or dic[ch] != stack[-1]:
1414
return False
1515
stack.pop()
16-
1716
return len(stack) == 0

0 commit comments

Comments
 (0)