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. 题目思路: 私用是用两 ...
随机推荐
- codeforce-1201-C题解
题目:给你一个包含n个整数的数组A(n为奇数),对A做k次以下操作: 对数组排序使数组以非递减顺序排列. 选取数组的中位数,然后加一 最终使得数组的中位数最大. 输入:第一行输入两个数字 n 和 k ...
- java字符串,数组,集合框架重点
1.字符串的字面量是否自动生成一个字符串的变量? String str1 = “abc”; Sring str2 = new String (“abc”); 对于str1:Jvm在遇到双 ...
- Flask基础(06)-->视图常用逻辑
Flask基础(06)-->视图常用逻辑 返回json 重定向:url_for 自定义状态码 返回json:在使用 Flask 写一个接口时候需要给客户端返回 JSON 数据,在 Flask 中 ...
- CSS3相关编码规范
一.CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等)2.大小(width, height, padding, margin) ...
- MongoDB的复制源oplog
之前有说过MongoDB的复制是异步复制的,其实也就是通过oplog来实现的,他存放在local数据库中,我们来查询一下主节点的日志大小. 除了主节点有oplog之外,其他节点也就有oplog ...
- 前端get和post那些事
首先,简单介绍下,get和post请求方法,综合以往笔记,现整理如下: 一.HTTP请求比较: 两种在客户端和服务器端进行请求-响应的方法是:GET和POST. GET - 从指定的资源请求数据 PO ...
- 理解 Redux 的中间件
将该思想抽象出来,其实和 Redux 就无关了.问题变成,怎样实现在截获函数的执行,以在其执行前后添加自己的逻辑. 为了演示,我们准备如下的示例代码来模拟 Redux dispatch action ...
- Java诊断利器Arthas
1 简介 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱 当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决: 这个类从哪个 jar 包加载的?为什么会报各种类相关的 ...
- ELK 学习笔记之 elasticsearch基本概念和CRUD
elasticsearch基本概念和CRUD: 基本概念: CRUD: 创建索引: curl -XPUT 'http://192.168.1.151:9200/library/' -d '{" ...
- PHP 上传文件限制
随笔于新浪面试失败: 需要好好补补了 Windows 环境下的修改方法 ================================================================ ...