forked from Teingi/python-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_020.py
More file actions
46 lines (28 loc) · 792 Bytes
/
leetcode_020.py
File metadata and controls
46 lines (28 loc) · 792 Bytes
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
# -*- coding:utf-8 -*-
"""
给定一个字符串所表示的括号序列,包含以下字符:
'(', ')', '{', '}', '[' and ']'
判定是否是有效的括号序列。
"""
strs = list(input("请输入括号序列:"))
#print(strs)
def isValid(s):
if s is None:return False
str_x = ["(","[","{"]
str_y = [")","]","}"]
str_z = ["()","[]","{}"]
res = []
for i in s:
if i in str_x:
res.append(i)
elif i in str_y:
if res == []:
return False
else:
temp = res.pop(-1) + i
if temp not in str_z:
return False
if len(res) != 0:
return False
return True
print(isValid(strs))