一、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的更多相关文章

  1. Leetcode Tags(13)Tree

    1.前序.中序.后序递归方式遍历二叉树 public void preOrderRecur(Node T) { if (T != null) { System.out.print(T.val + &q ...

  2. Leetcode Tags(1)Linked List

    1.知识点回顾 https://www.cnblogs.com/BigJunOba/p/9174206.html https://www.cnblogs.com/BigJunOba/p/9174217 ...

  3. Leetcode Tags(13)Bit Manipulation

    一.477.汉明距离总和 输入: , , 输出: 解释: 在二进制表示中,4表示为0100,14表示为1110,2表示为0010.(这样表示是为了体现后四位之间关系) HammingDistance( ...

  4. Leetcode Tags(8)Binary Search

    一.475. Heaters 输入: [1,2,3],[2] 输出: 1 解释: 仅在位置2上有一个供暖器.如果我们将加热半径设为1,那么所有房屋就都能得到供暖. 输入: [1,2,3,4],[1,4 ...

  5. Leetcode Tags(6)Math

    一.204. Count Primes Count the number of prime numbers less than a non-negative number, n. Input: 10 ...

  6. Leetcode Tags(5)Hash Table

    一.500. Keyboard Row 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词. 输入: ["Hello", "Alaska", &q ...

  7. Leetcode Tags(3)String(TODO)

    一.Easy 696 Count Binary Substrings Input: "00110011" Output: 6 Explanation: There are 6 su ...

  8. Leetcode Tags(2)Array

    一.448. Find All Numbers Disappeared in an Array 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了 ...

  9. [LeetCode]题解(python):155-Min Stack

    题目来源: https://leetcode.com/problems/min-stack/ 题意分析: 实现一个小的栈,包括初始化,push,pop,top,和getMin. 题目思路: 私用是用两 ...

随机推荐

  1. jenkins+svn+Ant+tomcat+非maven项目构建

    首先,输入项目名称,创建一个自由风格的项目; 然后,配置旧项目的策略参数,目的是防止构建项目太多,占用资源. 下一步,jdk版本选择: 下一步,关联svn项目. 下一步:配置ant 看不清,再来一张. ...

  2. Java源码解析|String源码与常用方法

    String源码与常用方法 1.栗子 代码: public class JavaStringClass { public static void main(String[] args) { Strin ...

  3. Linux系列之yum安装

    yum是Linux系统的安装必备神器,简直不要太方便.但是新系统一般是不自带yum工具的,所以需要手动安装一下. 环境:centos7 新建一个目录用来保存yum安装包 mkdir install 进 ...

  4. 十大排序算法JavaScript实现总结

    花费了几周的时间断断续续的练习和模仿与使用JavaScript代码实现了十大排序算法. 里面有每种算法的动图和静态图片演示,看到图片可以自己先按照图片的思路实现一下. github中正文链接,点击查看 ...

  5. 一道短小精悍的JS小题目

    看到题目你是不是以为我在开车,没错,我就不承认,你能咋的,你瞅啥,别瞅我,瞅题 题目是这样式的 结果是啥呀,这里我就不买关子了,结果是undefined,相信基础好一点的大佬们一眼就能看出来,像我这种 ...

  6. Python多任务之线程

    多任务介绍 我们先来看一下没有多任务的程序 import time def sing(): for i in range(5): print("我喜欢唱") time.sleep( ...

  7. [scrapy-redis] install and configure scrapy-redis on CentOS 7 (1)

    0. 安装依赖 yum install -y zlib zlib-devel openssl openssl-devel bzip2 bzip2-devel sqlite-devel gcc wget ...

  8. 如何选择一款适合自己操作系统、Windows、Mac还是Linux?

    如何选择一款适合自己操作系统.Windows.Mac还是Linux? 作者:我们都很努力着 简介:电脑已经逐渐离不开我们生活,但是如何选择一个我们最佳,最适合的电脑操作系统就成了一些困难选择人士的一个 ...

  9. 如何写md格式的文档

    一.标题 标题其实和HTML中的h系列很像,想要设置为标题的文字前面加#来表示一个#是一级标题,二个#是二级标题,以此类推.支持六级标题. 注:标准语法一般在#后跟个空格再写文字, 示例: 效果如下: ...

  10. MySQL 视图(合并多表数据)引发的严重性能问题

    问题背景: 一.客户环境连续多次出现性能问题,系统登入异常,数据库CPU告警. 处理过程: 1>协助排查数据库性能问题时发现如下两个较频繁的SQL导致严重的性能问题(均使用了视图合并多表数据): ...