offer 09 offer 30 if (help.isEmpty()) help.push(x); if (x<=help.peek()) help.push(x); 应优化成 if (help.isEmpty()||x<=help.peek()) help.push(x);
Deque<> stk=new LinkedList()定义栈
如
Deque<Integer> deque,help; deque=new LinkedList(); help=new LinkedList();
pop出,push入,peek获取栈顶,isEmpty()判断是否非空
要在时间复杂度为O(1)的情况下得到栈的最小值,可以使用辅助栈的方式,在入栈出栈的同时对辅助栈也进行操作,最后取辅助栈栈顶。
两个栈可以实现一个队列。(我暂时感觉p用没有,直接一个链表不就行了)
deque.peek()==help.peek()
应优化成
Objects.equals(deque.peek(), help.peek())