Leetcode Tags(4)Stack & Queue
一、232. Implement Queue using Stacks
private Stack<Integer> stack; /** Initialize your data structure here. */
public e232() {
stack = new Stack<>();
} /** Push element x to the back of queue. */
public void push(int x) {
stack.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
Stack<Integer> tmp = new Stack<>();
while (!stack.isEmpty()) tmp.push(stack.pop());
int result = tmp.pop();
while (!tmp.isEmpty()) stack.push(tmp.pop());
return result;
} /** Get the front element. */
public int peek() {
Stack<Integer> tmp = new Stack<>();
while (!stack.isEmpty()) tmp.push(stack.pop());
System.out.println(tmp);
int result = tmp.peek();
System.out.println(result);
while (!tmp.isEmpty()) stack.push(tmp.pop());
return result;
} /** Returns whether the queue is empty. */
public boolean empty() {
return stack.isEmpty();
}
二、225. Implement Stack using Queues
private Queue<Integer> queue; /** Initialize your data structure here. */
public e225() {
queue = new ArrayDeque<>();
} /** Push element x onto stack. */
public void push(int x) {
queue.add(x);
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
Queue<Integer> tmp = new ArrayDeque<>();
while (queue.size() != 1) tmp.add(queue.poll());
int result = queue.peek();
queue = tmp;
return result; } /** Get the top element. */
public int top() {
Queue<Integer> tmp = new ArrayDeque<>();
while (queue.size() != 1) {
tmp.add(queue.poll());
}
int result = queue.peek();
tmp.add(result);
queue = tmp;
return result;
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
三、155. Min Stack
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
思路:使用两个栈,一个用来存值,另一个用来存在当前值压入栈后的最小值。
private Stack<Integer> stack1 ;
private Stack<Integer> stack2 ;
private int min; /** initialize your data structure here. */
public MinStack() {
stack1 = new Stack<>();
stack2 = new Stack<>();
min = Integer.MAX_VALUE;
} public void push(int x) {
stack1.push(x);
min = Math.min(min, x);
stack2.push(min);
} public void pop() {
stack1.pop();
stack2.pop();
if (stack2.isEmpty()) {
min = Integer.MAX_VALUE;
} else {
min = stack2.peek();
}
} public int top() {
return stack1.peek();
} public int getMin() {
return stack2.peek();
}
四、739. Daily Temperatures
五、
Leetcode Tags(4)Stack & Queue的更多相关文章
- Leetcode Tags(13)Tree
1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + &q ...
- Leetcode Tags(1)Linked List
1.知识点回顾 https://www.cnblogs.com/BigJunOba/p/9174206.html https://www.cnblogs.com/BigJunOba/p/9174217 ...
- Leetcode Tags(13)Bit Manipulation
一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...
- Leetcode Tags(8)Binary Search
一.475. Heaters 输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器.如果我们将加热半径设为1,那么所有房屋就都能得到供暖. 输入: [1,2,3,4],[1,4 ...
- Leetcode Tags(6)Math
一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...
- Leetcode Tags(5)Hash Table
一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...
- Leetcode Tags(3)String(TODO)
一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...
- Leetcode Tags(2)Array
一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...
- [LeetCode]题解(python):155-Min Stack
题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin. 题目思路: 私用是用两 ...
随机推荐
- 数据结构之二叉树篇卷三 -- 二叉树非递归遍历(With Java)
Nonrecursive Traversal of Binary Tree First I wanna talk about why we should <code>Stack</c ...
- unzip 命令指定解压路径
在使用unzip进行文件包解压,可以用来解压zip/jar/war包类型,有时解压时需要解压到指定路径时可以使用参数 -d 来指定,例如: unzip services-bak.jar -d ./we ...
- springboot 使用i18n进行国际化乱码解决
方式1.设置国际化的编码和你使用的编译器(IDEA之类)一致,如编译器为UTF-8则在application配置文件中添加 #i18n spring: messages: encoding: UTF- ...
- python爬虫添加请求头和请求主体
添加头部信息有两种方法 1.通过添加urllib.request.Request中的headers参数 #先把要用到的信息放到一个字典中 headers = {} headers['User-Agen ...
- .NET进阶篇-丑话先说,Flag先立
作为开发者,工作了几年,也总觉得技术栈和刚毕业区别不大,用的技术还都是N年前的,每每看到新东西,也只心里哇塞惊叹一下,然后就回归于忙碌.怪自己的技术池太浅,热门的令人称奇的技术也都是在其他巨人的肩膀上 ...
- ueditor的初始化赋值
ue.ready(function () {ue.setContent('初始内容'); //赋值给UEditor });
- ShaderHelper2 组件升级,支持自动枚举参数!
ShaderHelper2 组件新体验视频演示: https://www.bilibili.com/video/av69314195/ ShaderHelper2 组件我们已经介绍过两次了,不了解的伙 ...
- Java8 Stream性能如何及评测工具推荐
作为技术人员,学习新知识是基本功课.有些知识是不得不学,有些知识是学了之后如虎添翼,Java8的Stream就是兼具两者的知识.不学看不懂,学了写起代码来如虎添翼. 在上篇<Java8 Stre ...
- Android 总结:ContentProvider 的使用
一.概述 ContentProvider:为存储和获取数据提供统一的接口,可以在不同的应用程序之间共享数据. Android内置的许多数据都是使用ContentProvider形式,供开发者调用的 ( ...
- Tomcat部署项目的三个方法
所需软件安装 要想在Tomcat中部署项目前提是先要搭建好Tomcat,搭建Tomcat就离不开以下软件包的安装配置,本次演示使用Linux平台 1.JDK软件包 JDK是一切java应用程序的基础, ...