deftest1():
l =[]for i inrange(1000):
l +=[i]deftest2():
l =[]for i inrange(1000):
l.append(i)deftest3():
l =[i for i inrange(1000)]deftest4():
l =list(range(1000))
s = Stack()
s.push("a")
s.push('b')
s.push('C')print(s.size())print(s.pop())print(s.size())print(s.pop())
3
C
2
b
使用栈解决括号匹配问题
deffunc(symbolStr):
s = Stack()
stillOK =True
index =0while index <len(symbolStr)and stillOK:
symbol = symbolStr[index]if symbol =='(':
s.push(symbol)else:if s.isEmpty():
stillOK =Falseelse:
s.pop()
index +=1if stillOK and s.isEmpty():returnTrueelse:returnFalse
func('(((())))')
True
func("(((()))")
False
deffunc(symbolStr):
s = Stack()
stillOK =True
index =0while index <len(symbolStr)and stillOK:
symbol = symbolStr[index]if symbol in'({[':
s.push(symbol)else:if s.isEmpty():
stillOK =Falseelse:
top = s.pop()ifnot match(top, symbol):
stillOK =False
index +=1if stillOK and s.isEmpty():returnTrueelse:returnFalsedefmatch(Open , Close):
opens ="{[("
closes ="}])"return opens.index(Open)== closes.index(Close)
print(func("{{([][])}()}"))print(func('[{()]'))
True
False
deffunc(num):'''将一个数转换为二进制'''
s = Stack()while num >0:
res = num %2
s.push(res)
num = num //2
Str =""whilenot s.isEmpty():
Str = Str +str(s.pop())return Str
print(func(9))print(func(14))print(func(16))
1001
1110
10000
deffunc(num, base):'''将一个数转换为任意进制'''
digits ="0123456789ABCDEF"
s = Stack()while num >0:
res = num % base
s.push(res)
num = num // base
Str =""whilenot s.isEmpty():
Str = Str + digits[s.pop()]return Str
defhotPotato(nameList,num):
q = Queue()for name in nameList:
q.enqueue(name)while q.size()>1:for i inrange(num):
q.enqueue(q.dequeue())
q.dequeue()return q.dequeue()
deffunc(astring):
d = Deque()for char in astring:
d.addFront(char)
stillOK =Truewhile d.size()>1and stillOK:
front = d.removeFront()
last = d.removeRear()if front != last:
stillOK =Falsereturn stillOK
A --> B
A --> C
B --> C
A --> B
C --> A
C --> B
A --> B
A --> C
B --> C
B --> A
C --> A
B --> C
A --> B
A --> C
B --> C
defdigui(num):print('$'+str(num))# 临界值if num >0:# 这里用的是调用本身的函数(递推关系)
digui(num-1)else:print('='*20)print(num)
digui(3)
$3
$2
$1
$0
====================
0
1
2
3
defmove(n, a, b, c):if n ==1:print(a,'==>', c)else:
move(n-1, a, c, b)
move(1, a, b, c)
move(n-1, b, a, c)
move(4,"A","B","C")
A ==> B
A ==> C
B ==> C
A ==> B
C ==> A
C ==> B
A ==> B
A ==> C
B ==> C
B ==> A
C ==> A
B ==> C
A ==> B
A ==> C
B ==> C
找零钱
defrecMC(coinValueList,change):'''该方法会存在大量的重复计算,耗时'''
minCoins = change
if change in coinValueList:return1else:for i in[c for c in coinValueList if c <= change]:
numCoins =1+ recMC(coinValueList,change-i)if numCoins < minCoins:
minCoins = numCoins
return minCoins
print(recMC([1,5,21,25],63))
3
defrecMC(coinValueList, change, knownResults):
minCoins = change
if change in coinValueList:
knownResults[change]=1elif knownResults[change]>0:return knownResults[change]else:for i in[c for c in coinValueList if c < change]:
numCoins =1+ recMC(coinValueList, change - i, knownResults)if numCoins < minCoins:
minCoins = numCoins
knownResults[change]= minCoins
# print(knownResults)return minCoins
print(recMC([1,5,21,25],63,[0]*64))
7
动态规划找零钱
defdpMakeChange(coinValueList,change,minCoins,coinsUsed):for cents inrange(change+1):
coinCount = cents
newCoin =1for j in[c for c in coinValueList if c <= cents]:if minCoins[cents-j]+1< coinCount:
coinCount = minCoins[cents-j]+1
newCoin = j
minCoins[cents]= coinCount
coinsUsed[cents]= newCoin
return minCoins[change]defprintCoins(coinsUsed,change):
coin = change
while coin >0:
thisCoin = coinsUsed[coin]print(thisCoin)
coin = coin - thisCoin
defmain():
amnt =65
clist =[1,5,10,21,25]
coinsUsed =[0]*(amnt+1)
coinCount =[0]*(amnt+1)print("Making change for",amnt,"requires")print(dpMakeChange(clist,amnt,coinCount,coinsUsed),"coins")print("They are:")
printCoins(coinsUsed,amnt)print("The used list is as follows:")print(coinsUsed)
C="abcdefhe"
D="abbcdefghe"
m=0
n=len(C)
E=[]
b=0while(m<n):
i=n-m
while(i>=0):
E.append(C[m:m+i])
i-=1
m+=1for x in E:
a=0if x in D:
a=len(x)
c=E.index(x)if a > b:#保存符合要求的最长字符串长度和地址
b=a
d=c
if b>0:print(E[d])