Open ylqi007 opened 9 months ago
+, -, *, /
]+, -
and (, )
]+, -, *, /
and (, )
]https://leetcode.com/tag/monotonic-stack/ A monotonic stack is simply a stack where the elements are always in sorted order. A monotonic stack is a stack in which the elements are always sorted. A stack can be monotonically increasing (sorted ascending) or monotonically decreasing (sorted descending).
以下前四种traversal都有recursion和iteration两种写法
p
or q
can be null
]
left.val < root.val < right.val
]p
or q
cannot be null
]p
and q
exist. Two Pointers]
Queue (队列)
Queue是线性表的一种,在操作数据元素时,按照先进先出(First In First Out, FIFO)的规则。
Queue的实现方式: 队列的实现同样有两种方式:顺序存储和链式存储。两者的区别在于数据元素在物理存储结构上的不同。
1. 顺序存储
顺序存储存在的问题: 由于数组申请的空间有限,到某一时间点,就会出现
rear
队尾指针到了数组的最后一个存储位置,如果继续存储,由于rear
指针无法后移,则会出错。顺序存储的升级版:使用数组存取数据元素时,可以将数组申请的空间想象成首尾连接的环状空间使用。
如何区分空队列和满队列的情况?办法是:牺牲掉一个存储空间
rear
等于头指针front
rear
的下一个位置等于头指针front
2. 链式存储
队列的链式存储是在链表的基础上,按照“先进先出”的原则操作数据元素。
Reference