496 下一个最大的元素
方法1 :自己瞎写的 没有考虑到栈的特性
class Solution:
def nextGreaterElement(self, nums1, nums2):
L=[]
for i in nums1:
k=nums2.index(i)
lenth2=len(nums2)
if k==lenth2-1:
L.append(-1)
for j in range(k+1,lenth2):
if nums2[j]>i:
L.append(nums2[j])
break
if j==lenth2-1:
L.append(-1)
return L
方法2:网上大神写的 很膜拜
class Solution:
def nextGreaterElement(self, nums1, nums2):
d = {}
st = []
ans = []
for x in nums2:
while len(st) and st[-1] < x:
d[st.pop()] = x
st.append(x)
for x in nums1:
ans.append(d.get(x, -1))
return ans
682.棒球比赛
注:好久前写的,后面再吧栈的写出来
ops= ["35","-32","C","39","+","-48","C","5","88","-52","+","C","D","D","100","C","+","-74","53","59"]
L=[]
sum=[]
lenth=len(ops)
for i in range(0,lenth):
if i==0:
sum.append(int(ops[i]))
else:
if ops[i]==‘C‘:
sum.insert(0,0)
sum.insert(1,0)
sum.pop()
elif ops[i]==‘D‘:
sum.append(sum[i-1]*2)
elif ops[i]==‘+‘:
sum.append(sum[i-1]+sum[i-2])
else :
sum.append(int(ops[i]))
score=0
for j in sum:
score=score+j
print(score)
232.用栈实现队列
方法1:网上的,用两个list来实现
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.stact=[]
self.queue=[]
self.len=0
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.stact.append(x)
self.len=self.len+1
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
while len(self.stact)!=0:
self.queue.append(self.stact.pop())
x = self.queue.pop()
while len(self.queue)!= 0:
self.stact.append(self.queue.pop())
self.len -=1
return x
def peek(self):
"""
Get the front element.
:rtype: int
"""
while len(self.stact)!=0:
self.queue.append(self.stact.pop())
x = self.queue.pop()
self.queue.append(x)
while len(self.queue)!= 0:
self.stact.append(self.queue.pop())
return x
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
if self.len==0:
return False
方法2:用一个list来实现,比较简单
class MyQueue:
def __init__(self):
"""
Initialize your data structure here.
"""
self.queue = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.queue.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if len(self.queue)>0:
print( self.queue.pop(0))
def peek(self):
"""
Get the front element.
:rtype: int
"""
if len(self.queue)>0:
print( self.queue[0])
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
if len(self.queue)>0:
return False
else:
return True
def shuchu(self):
for i in self.queue:
print(i)
235,用队列来实现栈
注:与上面异曲同工
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.queue = []
def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.queue.append(x)
def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
if len(self.queue)>0:
return self.queue.pop()
#len(self.queue)-=1
def top(self):
"""
Get the front element.
:rtype: int
"""
if len(self.queue)>0:
return self.queue[-1]
def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
if len(self.queue)>0:
return False
else:
return True
155,最小栈
注:可以使用深复制在里面找出来最小值,但会超时,很慢很慢
方法1:用min 函数,较慢,因为每次都执行
class MinStack:
def __init__(self):
"""
initialize your data structure here.
"""
self.queue = []
self.min=None
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.queue.append(x)
self.min=min(self.queue)
def pop(self):
"""
:rtype: void
"""
if len(self.queue)>0:
self.queue.pop()
self.min=min(self.queue)
return self.queue
def top(self):
"""
:rtype: int
"""
if len(self.queue)>0:
return self.queue[-1]
def getMin(self):
"""
:rtype: int
"""
return self.min
方法2:判断,比较快
class MinStack(object):
def init(self):
"""
initialize your data structure here.
"""
self.stack = []
self.min = None
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.stack.append(x)
if self.min == None or self.min > x:
self.min = x
def pop(self):
"""
:rtype: void
"""
popItem = self.stack.pop()
if len(self.stack) == 0:
self.min = None
return popItem
if popItem == self.min:
self.min = self.stack[0]
for i in self.stack:
if i < self.min:
self.min = i
return popItem
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: int
"""
return self.min
844.比较含退格的字符串
注:注意遇到‘#’时,字符串长度为0时的处理,以及两个“#”,字符串长度为0时的处理。PS,这是老子自己写出来运行最快的代码了,代码也简洁,写了半个小时吧,有一丢丢自豪。还学会了在类里调用函数。一个类里不能函数调用函数。
class Solution:
def backspaceCompare(self, S, T):
"""
:type S: str
:type T: str
:rtype: bool
"""
S1=back(S)
T1=back(T)
if S1==T1:
return True
else:
return False
def back(L):
K=[]
for i in L:
if i==‘#‘ and len(K)!=0:
K.pop()
elif i==‘#‘ and len(K)==0:
pass
else:
K.append(i)
return K
20,有效的括号
注:好久前写的了,也是在学栈之前写的,后面把栈的写出来。
class Solution:
def isValid(self, s):
a=list(s)
b=[] #存放左括号的栈 qc:list当做栈
c={‘(‘:‘)‘,‘[‘:‘]‘,‘{‘:‘}‘} #字典存储 qc;key:value 键:值
for i in a:
if i==‘‘:
return True
elif i in c: #如果是字典中的键,即左括号,放进栈
b.append(i)
else:
if len(b)==0: #先判断是否有左括号存在
return False
else:
#字典得到该键的值==栈顶值对应的右括号
if c.get(b[-1])!=i:
return False
else:
del b[-1] #删除栈顶元素
if len(b)!=0: #若还存在左括号,此时已没有右括号,出错
return False
return True
PS.写这么一些题还是有用的,至少遇到一些题知道其实是采用了栈的思想,以前都是瞎写。编程还是要有思维。
原文地址:http://blog.51cto.com/13930723/2327301