MyStack.size() == from.size(),因为 to 为空队列,只是函数过程可能暂存一下。
push 要特别注意,直接 push 到 to 里,然后把 from 的元素逐个 push 到 to 中,此时 to 为实际上的栈,然后再 from & to 互换引用。
/*** 思路:使用辅助队列,每次push都替一下,保证实际上 queue1 是按照栈顺序来的* 还是有点类似 JVM GC 的复制算法
*/classMyStack{LinkedList<Integer> from;LinkedList<Integer>to;/** Initialize your data structure here. */publicMyStack(){from =newLinkedList<>();to=newLinkedList<>();}// add:相当于 Queue.push()// removeFirst: 相当于 Queue.pop()/** Push element x onto stack. */publicvoidpush(int x){to.add(x);while(!from.isEmpty()){to.add(from.removeFirst());}LinkedList<Integer> temp = from;from =to;to= temp;}/** Removes the element on top of the stack and returns that element. */publicintpop(){return from.removeFirst();}/** Get the top element. */publicinttop(){// 相当于 Queue.peek()return from.get(0);}/** Returns whether the stack is empty. */publicbooleanempty(){return from.isEmpty();}}/*** Your MyStack object will be instantiated and called as such:* MyStack obj = new MyStack();* obj.push(x);* int param_2 = obj.pop();* int param_3 = obj.top();* boolean param_4 = obj.empty();*/
1M等于多少字节?以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!1M等于多少字节?不是1M等于多少字节,是1MB等于多少字节。字节(Byte /bait/ n. [C])是计算机信息技术用于计量存…