-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathExample01_D.py
More file actions
51 lines (42 loc) · 1.46 KB
/
Example01_D.py
File metadata and controls
51 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
class Solution:
def isValid(self, s):
# 当字符串本来就是空的时候,我们可以快速返回true
if not s and len(s) == 0:
return True
# 当字符串长度为奇数的时候,不可能是一个有效的合法字符串
if len(s) % 2 == 1:
return False
# 消除法的主要核心逻辑:
leftBraceNumber = 0
for c in s:
# 取出字符
if c == '(':
# 如果是'(',那么压栈
leftBraceNumber += 1
elif c == ')':
# 如果是')',那么就尝试弹栈
if leftBraceNumber <= 0:
# 如果弹栈失败,那么返回false
return False
leftBraceNumber -= 1
return leftBraceNumber == 0
solution = Solution()
assert solution.isValid("")
assert not solution.isValid("(")
assert not solution.isValid(")")
assert solution.isValid("()")
assert not solution.isValid("((")
assert not solution.isValid("))")
assert not solution.isValid(")(")
assert not solution.isValid("())")
assert not solution.isValid("(((")
assert not solution.isValid(")))")
assert not solution.isValid(")()")
assert solution.isValid("()()")
assert solution.isValid("(())")
assert not solution.isValid("))((")
assert solution.isValid("()()()")
assert solution.isValid("((()))")
assert solution.isValid("()(())")
assert not solution.isValid("()(()(")