一、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. [python]汉诺塔问题

    相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏.该游戏是在一块铜板装置上,有三根杆(编号A.B.C),在A杆自下而上.由大到小按顺序放置64个金盘(如下图).游戏的目标:把A杆上的金盘全部 ...

  2. 全文搜索引擎 Elasticsearch

    写在前面 最近在学Elasticsearch , 我相信只要是接触过开发的都会听过Elasticsearch或ELK这么一个技术. 主要用来做全文检索或大数据分析等,之前一直处理了解状态. 所以打算系 ...

  3. JMeter 压测Server Agent无法监控资源问题,PerfMon Metrics Collector报Waiting for sample,Error loading results file - see file log, Can't accept UDP connections java.net.BindException: Address already in use 各种疑难杂症

    如何安装插件此博主已经说得很详细了. https://www.cnblogs.com/saryli/p/6596647.html 但是需注意几点: 1.修改默认端口,这样可以避免掉一个问题.Serve ...

  4. Redis数据库之服务器主从配置

    目的 主要培养对分布式REDIS主从复制架构运用的能力.理解并掌握REPLICATION工作原理的同时,能独立配置Replication ,使数据库运行在主从架格上.针对主从复制架构的运用,着力掌握S ...

  5. Stanford公开课《编译原理》学习笔记(2)递归下降法

    目录 一. Parse阶段 CFG Recursive Descent(递归下降遍历) 二. 递归下降遍历 2.1 预备知识 2.2 多行语句的处理思路 2.3 简易的文法定义 2.4 文法产生式的代 ...

  6. ThinkPHP5通过composer安装Workerman安装失败问题(避坑指南)

    $ composer require topthink/think-workerUsing version ^2.0 for topthink/think-worker./composer.json ...

  7. MongoDB 学习笔记之 分析器和explain

    MongoDB分析器: 检测MongoDB分析器是否打开: db.getProfilingLevel() 0表示没有打开 1表示打开了,并且如果查询的执行时间超过了第二个参数毫秒(ms)为单位的最大查 ...

  8. 用js做数字字母混合的随机四位验证码

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. css3:bacground-size

    个人博客: https://chenjiahao.xyz CSS3之背景尺寸Background-size是CSS3中新加的一个有关背景的属性,这个属性是改变背景尺寸的通过各种不同是属性值改变背景尺寸 ...

  10. 为了给女朋友买件心怡内衣,我用Python爬虫了天猫内衣售卖数据

    真爱,请置顶或星标 大家好,希望各位能怀着正直.严谨.专业的心态观看这篇文章.ヾ(๑╹◡╹)ノ" 接下来我们尝试用 Python 抓取天猫内衣销售数据,并分析得到中国女性普遍的罩杯数据.最受 ...