[LeetCode]Implement Stack using Queues
2015-12-05 13:34
337 查看
class MyStack { // Push element x onto stack. Queue<Integer> queue = new LinkedList<Integer>(); public void push(int x) { Queue<Integer> q = new LinkedList<Integer>(); q.offer(x); while (!queue.isEmpty()) { q.offer(queue.poll()); } queue = q; } // Removes the element on top of the stack. public void pop() { queue.poll(); } // Get the top element. public int top() { return queue.peek(); } // Return whether the stack is empty. public boolean empty() { return queue.isEmpty(); } }
相关文章推荐
- 使用UI Automation实现自动化测试 (InvokePattern)
- (译)如何自定义UIDocument的子类
- DDuilib入门【一】
- Ubuntu 15.10下android studio 1.5,新建android项目,gradle build running永不停止
- UIScrollView基本用法
- 第25篇 jQuer快速学习(上)---选择器和DOM操作
- 爬爬爬之路:UI(十五) KVO
- Code Forces 592 B. The Monster and the Squirrel(水~)
- android Bluetooth开发基本流程
- poj 1679 The Unique MST(次小生成树变形)
- ios_Cocoa框架 “Foundation”和“UIKit”的区别
- android Bluetooth的实现蓝牙操作
- UIView(包括子类)的几个初始化时执行动作的时机
- Leetcode Longest Increasing Subsequence
- Code Forces 591 A. Wizards' Duel(水~)
- OC-7.NSSet NSValue NSNumber
- block bio queue request等
- 45.KVO(Key-Value-Observer)键值观察者
- UIPageControl控件的常用属性
- 黑马程序员-Java中只读类String和StringBuffer、StringBuilder类)