Implement Stack using Queues ——LeetCode
Implement the following operations of a stack using queues.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- empty() -- Return whether the stack is empty.
Notes:
- You must use only standard operations of a queue -- which means only
push to back
,peek/pop from front
,size
, andis empty
operations are valid. - Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
- You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
题目大意:用队列,实现栈。
class MyStack { List<Integer> stack = new ArrayList<>(); // Push element x onto stack.
public void push(int x) {
stack.add(x);
} // Removes the element on top of the stack.
public void pop() {
if(!empty()){
stack.remove(stack.size()-1);
}
} // Get the top element.
public int top() {
if(!empty()){
return stack.get(stack.size()-1);
}
return -1;
} // Return whether the stack is empty.
public boolean empty() {
return stack.size()==0;
}
}
Implement Stack using Queues ——LeetCode的更多相关文章
- Implement Stack using Queues leetcode
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues
232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 【一天一道LeetCode】#225. Implement Stack using Queues
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
随机推荐
- php中调用其他系统http接口的方法说明
使用函数: file_get_contents($url); 传入接口url及其参数:如 $url="http://192.168.1.1/test.jsp?id=1&type=2& ...
- MySQL 5.6 my.cnf 参数详细说明
# 以下选项会被MySQL客户端应用读取.# 注意只有MySQL附带的客户端应用程序保证可以读取这段内容.# 如果你想你自己的MySQL应用程序获取这些值.# 需要在MySQL客户端库初始化的时候指定 ...
- 添加Appicon的方法
1.将设计好的图片,拖拽到Groups&Files的Resources目录下: 2.修改Resources目录下的“工程名-info.plist”文件 3.修改该文件的Iconfile属性,填 ...
- ASP.NET网站实现中英文转换(本地化资源)
主要内容: 1. 简单例子 2. 进一步认识Localization 3. 语言转换 4. 解决方案 一. 简单例子 下面通过一个简单的例子来说明利用Localization来实现本地化是那么的简单, ...
- linux指令备份
ls -a 显示隐藏文件 cd 回到当前用户的目录 /home/ubuntu touch 创建文件 cat Hello.javamore/less Hello.java分页显示 grep root / ...
- hbase的存储体系
一.了解hbase的存储体系. hbase的存储体系核心的有Split机制,Flush机制和Compact机制. 1.split机制 每一个hbase的table表在刚刚开始的时候,只有一个regio ...
- yii 验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),
//验证用户名是否存在 array("name","unique",'message'=>'用户名已经存在'),
- MySQL账号授权操作
Mysql权限控制 - 允许用户远程连接 设置mysql root密码: mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = P ...
- openerp import l field size limit
modify the file addons/base_import/models.py add the following line at the very begining of the _con ...
- 使用OpenSSL API进行安全编程
http://www.ibm.com/developerworks/cn/linux/l-openssl.html OpenSSL API 的文档有些含糊不清.因为还没有多少关于 OpenSSL 使用 ...