dushine00 2020-01-12
栈是一个数据集合,可以理解为只能在一端进行插入或者删除操作的列表。
栈的特点:后进先出
栈的基本操作
def brace_match(s): stack = [] d = {"(":")", "[":"]", "{":"}"} for ch in s: if ch in {'(','[','{'}: stack.append(ch) elif len(stack) == 0: print('多了右括号%s' % ch) return False elif d[stack[-1]] == ch: stack.pop() else: print('括号%s处不匹配' % ch) return False if len(stack) == 0: return True else: print('剩余括号未匹配') return False
队列的理解:
队列的内置模块:from collections import deque
# 递归: def my_num(x): if x == 1: return 1 elif x == 2: return 2 else: return my_num(x-2) + my_num(x-1) for i in range(1, 10): print(my_num(i))
def VehicleReecorder(Trucks, k): BufferRails = [] for i in range(k): BufferRails.append([]) currentCarriage = 1 for i in Trucks: if i == currentCarriage: print(f'{i}') currentCarriage += 1 continue else: for buffer in BufferRails: if not buffer: buffer.append(i) break else: if min(buffer) > i: buffer.append(i) break for buffer_list in BufferRails: for i in range(len(buffer_list)): last = buffer_list.pop() if last == currentCarriage: print(f'{i}') currentCarriage +=1