来看下Leetcode中Tag为Stack的题目
class Solution(object):
def dailyTemperatures(self, temperatures):
"""
:type temperatures: List[int]
:rtype: List[int]
"""
size,stack = len(temperatures),[]
ret = [0]*size
for idx,tem in enumerate(temperatures):
while(stack and temperatures[stack[-1]]return ret
class Solution(object):
def nextGreaterElement(self, findNums, nums):
"""
:type findNums: List[int]
:type nums: List[int]
:rtype: List[int]
"""
dmap,stack = {},[]
for num in nums:
while stack and stack[-1]return [dmap.get(n,-1) for n in findNums]
class Solution(object):
def nextGreaterElements(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
size,stack = len(nums),[]
ret = [-1]*size
nums = nums+nums
for idx,num in enumerate(nums):
while stack and nums[stack[-1]]if idjreturn ret
class Solution(object):
def calPoints(self, ops):
"""
:type ops: List[str]
:rtype: int
"""
stack = []
for op in ops:
if op=='C':
stack.pop()
elif op=='D':
stack.append(stack[-1]*2)
elif op=='+':
stack.append(stack[-1]+stack[-2])
else:
stack.append(int(op))
return sum(stack)
class Solution(object):
def asteroidCollision(self, asteroids):
"""
:type asteroids: List[int]
:rtype: List[int]
"""
stack = []
for asteroid in asteroids:
flag = True
while(stack and stack[-1]>0 and asteroid<0 and flag):
if -asteroid>stack[-1]: stack.pop()
else:
flag = False
if -asteroid==stack[-1]: stack.pop()
if flag: stack.append(asteroid)
return stack
class Solution(object):
def exclusiveTime(self, n, logs):
"""
:type n: int
:type logs: List[str]
:rtype: List[int]
"""
stack,ans = [],[0 for i in range(n)]
for log in logs:
logname,state,logtime = log.split(':')
logname,logtime = int(logname),int(logtime)
if state == 'start':
if len(stack):
ans[stack[-1][0]]+=logtime-stack[-1][1]
stack.append([logname,logtime])
else:
lastitem = stack.pop()
ans[lastitem[0]]+=logtime-lastitem[1]+1
if len(stack):
stack[-1][1]=logtime+1
return ans
class MyQueue(object):
def __init__(self):
self.stack1 = []
self.stack2 = []
def push(self, node):
self.stack1.append(node)
def pop(self):
if len(self.stack2):
return self.stack2.pop()
while(self.stack1):
self.stack2.append(self.stack1.pop())
return self.stack2.pop()
def peek(self):
if len(self.stack2):
return self.stack2[-1]
while(self.stack1):
self.stack2.append(self.stack1.pop())
return self.stack2[-1]
def empty(self):
return self.stack1==[] and self.stack2==[]
class MyStack(object):
def __init__(self):
"""
Initialize your data structure here.
"""
self.queue = []
self.size = 0
def push(self, x):
"""
Push element x onto stack.
:type x: int
:rtype: void
"""
self.queue.append(x)
self.size +=1
def pop(self):
"""
Removes the element on top of the stack and returns that element.
:rtype: int
"""
for i in range(self.size-1):
self.queue.append(self.queue.pop(0))
self.size -=1
return self.queue.pop(0)
def top(self):
"""
Get the top element.
:rtype: int
"""
for i in range(self.size-1):
self.queue.append(self.queue.pop(0))
target = self.queue.pop(0)
self.queue.append(target)
return target
def empty(self):
"""
Returns whether the stack is empty.
:rtype: bool
"""
return self.size == 0
class MinStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.stack = []
self.minstack =[]
def push(self, x):
"""
:type x: int
:rtype: void
"""
self.stack.append(x)
if len(self.minstack)==0 or self.minstack[-1][0]>x:
self.minstack.append((x,1))
elif self.minstack[-1][0] == x:
self.minstack[-1] = (x,self.minstack[-1][1]+1)
def pop(self):
"""
:rtype: void
"""
ans = self.stack[-1]
self.stack = self.stack[0:len(self.stack)-1]
if ans == self.minstack[-1][0]:
if self.minstack[-1][1] == 1:
self.minstack.remove(self.minstack[-1])
else:
self.minstack[-1] = (ans,self.minstack[-1][1]-1)
def top(self):
"""
:rtype: int
"""
return self.stack[-1]
def getMin(self):
"""
:rtype: int
"""
return self.minstack[-1][0]
class Solution(object):
def inorderTraversal(self,root): # iterative
'''
:param root: TreeNode
:return: List[int]
'''
stack,ret = [],[]
if root == None: return []
stack.append(root)
while(stack):
query = stack.pop()
if query.left == None and query.right == None:
ret.append(query.val)
else:
if query.right:
stack.append(query.right)
stack.append(TreeNode(query.val))
if query.left:
stack.append(query.left)
return ret
上述的空间复杂度是O(size),拍脑袋(其实是看别人的博客啦·)想到一个O(k)k是树深的想法,维护一个栈,从根节点开始,每次迭代地将根节点的左孩子压入栈,直到左孩子为空为止。调用next()方法时,弹出栈顶,如果被弹出的元素拥有右孩子,则以右孩子为根,将其左孩子迭代压栈。书影博客
class Solution(object):
def inorderTraversal(self,root): # iterative
'''
:param root: TreeNode
:return: List[int]
'''
stack,ret = [],[]
if root is None: return []
stack.append(root)
while(root):
if root.left:
stack.append(root.left)
root = root.left
while(stack):
root = stack.pop()
ret.append(root.val)
root = root.right
while(root):
stack.append(root)
root = root.left
return ret
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if root == None:
return []
ret = []
stack = [root]
while(len(stack)):
query = stack.pop()
ret.append(query.val)
if query.right:
stack.append(query.right)
if query.left:
stack.append(query.left)
return ret
class Solution(object):
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ret = []
if root == None:
return ret
stack = [root]
while(len(stack)):
query = stack.pop()
if query.left == None and query.right == None:
ret.append(query.val)
else:
stack.append(TreeNode(query.val))
if query.right:
stack.append(query.right)
if query.left:
stack.append(query.left)
return ret
class Solution(object):
def zigzagLevelOrder(self, root):
"""
:type root: TreeNode
:rtype: List[List[int]]
"""
ret, stack, step = [], [], 0
if root == None: return ret
stack.append(root)
while (stack):
tmpstack = []
tmp = []
for node in stack:
tmp.append(node.val)
if node.left:
tmpstack.append(node.left)
if node.right:
tmpstack.append(node.right)
if step % 2:
tmp = tmp[::-1]
ret.append(tmp[:])
step += 1
stack = tmpstack[:]
return ret
class Solution(object):
def evalRPN(self, tokens):
"""
:type tokens: List[str]
:rtype: int
"""
stack = []
for token in tokens:
if token in ['+','-','*','/']:
num1 = stack.pop()
num2 = stack.pop()
if token=='+':
stack.append(num1+num2)
elif token == '-':
stack.append(num2-num1)
elif token == '*':
stack.append(num1*num2)
else:
if num1*num2<0:
ans = -(-num2/num1)
else:
ans = num2/num1
stack.append(ans)
else:
stack.append(int(token))
return stack[0]
class Solution(object):
def isValidSerialization(self, preorder):
"""
:type preorder: str
:rtype: bool
"""
order = preorder.split(',')
if order == [] or order == ['#']: return True
stack,flag = [],True
for token in order:
if token == '#':
if stack == []: return False
stack[-1]+=1
while(stack and stack[-1]==2):
stack.pop()
if stack: stack[-1]+=1
else:
if stack == []:
if flag:
stack = [0]
flag = False
else:
return False
else:
stack.append(0)
return stack == []
class Solution(object):
def simplifyPath(self, path):
"""
:type path: str
:rtype: str
"""
path_array = path.split('/')
stack = []
for item in path_array:
if item not in ['','.','..']:
stack.append(item)
elif item == '..' and len(stack):
stack.pop(-1)
if stack ==[]:
return '/'
else:
return '/'+'/'.join(stack)
class Solution(object):
def isValid(self, s):
"""
:type s: str
:rtype: bool
"""
stack = []
dic = {')':'(',']':'[','}':'{'}
for i in s:
if i in ['(','[','{']:
stack.append(i)
elif i in [')',']','}']:
if len(stack) and stack[-1]==dic[i]:
stack.pop()
else:
return False
else:
return False
if len(stack)==0:
return True
return False
class NestedIterator(object):
def __init__(self, nestedList):
"""
Initialize your data structure here.
:type nestedList: List[NestedInteger]
"""
def getList(query):
query=query.getList()
ans = []
for i in range(len(query)):
if query[i].isInteger():
ans += [query[i]]
else:
ans += getList(query[i])
return ans
self.stack = []
for i in range(len(nestedList)):
if nestedList[i].isInteger():
self.stack += [nestedList[i]]
else:
self.stack += getList(nestedList[i])
def next(self):
"""
:rtype: int
"""
ret = self.stack.pop(0)
return ret.getInteger()
def hasNext(self):
"""
:rtype: bool
"""
return self.stack !=[]
class Solution(object):
def find132pattern(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
size,third,stack =len(nums),None,[]
for i in range(size):
num = nums[size-1-i]
if third and num < third:
return True
else:
while(len(stack) and num>stack[-1]):
third = stack[-1]
stack.pop()
stack.append(num)
return False
class Solution(object):
def removeKdigits(self, num, k):
"""
:type num: str
:type k: int
:rtype: str
"""
stack = []
length = len(num)-k
for i in num:
while len(stack) and k>0 and stack[-1]>i:
stack.pop()
k-=1
stack.append(i)
ret = ''
flag = True
for i in range(length):
if flag:
if stack[i]!='0':
ret+=stack[i]
flag =False
else:
ret +=stack[i]
if len(ret)==0:
return '0'
return ret
class Solution(object):
def decodeString(self, s):
"""
:type s: str
:rtype: str
"""
digit = None
stack = []
for si in s:
if si>='0' and si<='9':
if digit==None:
digit=int(si)
else:
digit=digit*10+int(si)
elif si=='[':
if digit!=None:
stack.append(digit)
digit=None
stack.append(si)
elif si==']':
tmp = ''
while(stack and stack[-1]!='['):
tmp = stack[-1]+tmp
stack.pop()
stack[-1] = tmp
if len(stack)>1 and isinstance(stack[-2],int):
stack[-2] = stack[-2]*stack[-1]
stack.pop()
else:
stack.append(si)
return ''.join(stack)
class Solution(object):
def deserialize(self, s):
"""
:type s: str
:rtype: NestedInteger
"""
stack = []
sigma, negmul = None, 1
for c in s:
if c == '-':
negmul = -1
elif '0' <= c <= '9':
sigma = (sigma or 0) * 10 + int(c)
elif c == '[':
stack.append(NestedInteger())
else:
if sigma is not None:
stack[-1].add(NestedInteger(sigma * negmul))
sigma, negmul = None, 1
if c == ']':
top = stack.pop()
if stack: stack[-1].add(top)
else: return top
return NestedInteger((sigma or 0) * negmul)
import collections
class Solution(object):
def countOfAtoms(self, formula):
"""
:type formula: str
:rtype: str
"""
size = len(formula)
def getDigit(idx):
cnt = 0
while (idx < len(formula) and formula[idx].isdigit()):
cnt = cnt*10+int(formula[idx])
idx += 1
idx-=1
if cnt:
return cnt,idx
else:
return 1,idx
stack = [collections.defaultdict(int)]
idx = 0
while(idxif token>='A' and token<='Z':
ch = token
idx+=1
while (idx < len(formula) and formula[idx]>='a' and formula[idx]<='z'):
ch+=formula[idx]
idx += 1
cnt,idx = getDigit(idx)
stack[-1][ch]=stack[-1][ch]+cnt
elif token=='(':
stack.append(collections.defaultdict(int))
elif token==')':
idx +=1
cnt,idx = getDigit(idx)
for key in stack[-1]:
stack[-1][key] = stack[-1][key]*cnt
tstack = stack.pop()
for key in tstack:
stack[-1][key]+=tstack[key]
idx+=1
ret = ''
for x in sorted(stack[-1].items(),key=lambda x:x[0]):
if x[1]!=1:
ret+=x[0]+str(x[1])
else:
ret+=x[0]
return ret