思路:
利用棧實(shí)現(xiàn)代數(shù)式中括號(hào)有效行的的檢驗(yàn):
代碼:
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
class mychain( object ): #利用鏈表建立棧,鏈表為父類(lèi) length = 0 def __init__( self ,value = None , next = None ): #創(chuàng)建鏈表,長(zhǎng)度并不包含頭部 self .value = value self . next = next #mychain.length=mychain.length+1 def append( self ,value = None ): while self . next ! = None : self = self . next self . next = mychain(value) mychain.length = mychain.length + 1 #追加時(shí),鏈表長(zhǎng)度增加 def travle( self ): #遍歷鏈表 print ( self .value) if self . next ! = None : self . next .travle() def drop ( self ,value): #刪除特定值的第一個(gè)匹配節(jié)點(diǎn) while self . next ! = None : if self . next .value! = value: self = self . next else : self . next = self . next . next mychain.length = mychain.length - 1 #刪除時(shí),鏈表長(zhǎng)度減小 break def pop( self ): #刪除未節(jié)點(diǎn) if self . next ! = None : #并不刪除頭結(jié)點(diǎn) while self . next . next ! = None : self = self . next self . next = None mychain.length = mychain.length - 1 #彈出為節(jié)點(diǎn),并減小長(zhǎng)度,頭結(jié)點(diǎn)不彈出 class stock(mychain): #棧類(lèi) bottom = None #棧底 top = None #棧頂 n_count = 0 #計(jì)數(shù) def Max ( self ): #占中最大值 if self . next ! = None : tmp = self . next .value while self . next . next ! = None : self = self . next if self . next .value>tmp: tmp = self . next .value return tmp else : print ( '棧為空!' ) def Min ( self ): #棧中的最小值 if self . next ! = None : tmp = self . next .value while self . next . next ! = None : self = self . next if self . next .value<tmp: tmp = self . next .value return tmp else : print ( '棧為空!' ) def push( self ,value): #壓棧 while self . next ! = None : self = self . next self . next = mychain(value) stock.top = self . next stock.length = stock.length + 1 stock.n_count = stock.n_count + 1 def __init__( self ,value = '', next = None ): self .value = value self . next = next stock.bottom = self stock.top = self #stock.n_count=stock.n_count+1 #stock.length=stock.length+1 def append( self ,value = ''): #取消追加函數(shù) print ( '請(qǐng)使用Push()!' ) def pop( self ): if self . next ! = None : #并不刪除頭結(jié)點(diǎn) while self . next . next ! = None : self = self . next self . next = None stock.top = self stock.length = stock.length - 1 #彈出為節(jié)點(diǎn),并減小長(zhǎng)度,頭結(jié)點(diǎn)不彈出 class solution( object ): def validationofbrackets( self ,astr = ''): #檢驗(yàn)串中的括號(hào)合法性 braketsstock = stock() for i in astr: if i in [ '{' , '(' , '[' ]: braketsstock.push(i) else : if i = = ')' : if braketsstock.top.value = = '(' : braketsstock.pop() else : return False elif i = = ']' : if braketsstock.top.value = = '[' : braketsstock.pop() else : return False elif i = = '}' : if braketsstock.top.value = = '{' : braketsstock.pop() else : return False else : pass print (astr) print (braketsstock.length) if braketsstock.length = = 0 : return True else : return False |
運(yùn)行:
1
2
3
|
bstr = '([{((({{}})))}]){{}}{{}{}{}[][]()(123)(((sin5)))}' f = solution() print (f.validationofbrackets(bstr)) |
總結(jié)
到此這篇關(guān)于python代數(shù)式括號(hào)有效性檢驗(yàn)的文章就介紹到這了,更多相關(guān)python代數(shù)式括號(hào)有效性檢驗(yàn)內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.cnblogs.com/wjqsdwm/p/13765413.html