Implement Queue using Stacks
来源:互联网 发布:手机淘宝客服中心 编辑:程序博客网 时间:2024/05/18 13:29
class MyQueue { // Using two stacks. Stack<Integer> s1 = new Stack<Integer>();// Pop out. Stack<Integer> s2 = new Stack<Integer>();// Cache. // Push element x to the back of queue. public void push(int x) { s2.push(x); } // Removes the element from in front of queue. public void pop() { if (empty()) { return; } if (s1.isEmpty()) { while (!s2.isEmpty()) { s1.push(s2.peek()); s2.pop(); } } s1.pop(); } // Get the front element. public int peek() { if (empty()) { return -1; } if (s1.isEmpty()) { while (!s2.isEmpty()) { s1.push(s2.peek()); s2.pop(); } } return s1.peek(); } // Return whether the queue is empty. public boolean empty() { return s1.isEmpty() && s2.isEmpty(); }}
0 0
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- Implement Queue using Stacks
- android post上传文件到服务器
- embed标签在页面加载的时候动态的改变src的值,可播放的
- 网站应加强个性化建设 提升SEO优化效果分析
- PCB设计线宽与电流
- CentOS6.4 安装GCC&G++编译器
- Implement Queue using Stacks
- 用PHP改变图片的尺寸大小
- Linux ipcs命令与ipcrm命令的用法详解
- float转16进制, 16进制转float(java )
- iOS判断字符串是否为汉字
- 1007. Maximum Subsequence Sum (25)
- DOM XSS案例总结
- GCD全称Grand Central Dispatch 是由苹果开发的一个多核编程的解决方案。网址:www.iphonetrain.com 公司:星宇无限(北京)科技有限公司
- unity中各种双击的实现