一、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. 基于MFCC的语音数据特征提取概述

    1. 概述 语音是人类之间沟通交流的最直接也是最快捷方便的一种手段,而实现人类与计算机之间畅通无阻的语音交流,一直是人类追求的一个梦想. 伴随着移动智能设备的普及,各家移动设备的厂家也开始在自家的设备 ...

  2. 检查图片是否损坏、图片后缀是否与实际图片类型对应 - Python

    图片工具 检查图片是否损坏 日常工作中,时常会需要用到图片,有时候图片在下载.解压过程中会损坏,而如果一张一张点击来检查就太不Cool了,因此我想大家都需要一个检查脚本: 测试图片,0.jpg是正常的 ...

  3. 005-做题:使用 Python 生成 200 个激活码

    题目:使用 Python 生成 200 个不重复的激活码 编写思路# 激活码一般是由26个大写字母和10个数字任意组合而成# 长度为12位或者16位的居多激活码# 一个激活码里的字符是可以重复的,而且 ...

  4. <机器学习>无监督学习算法总结

    本文仅对常见的无监督学习算法进行了简单讲述,其他的如自动编码器,受限玻尔兹曼机用于无监督学习,神经网络用于无监督学习等未包括.同时虽然整体上分为了聚类和降维两大类,但实际上这两类并非完全正交,很多地方 ...

  5. Win10 安装配置 MongoDB 4.0 踩坑记

    redis 官方没有 Windows 版的,微软维护的已经好久没更新了,所以就在想着换成 MongoDB. 于是一趟被我复杂化的踩坑之旅就开始了,同时也记录一下,避免有人遇见跟我一样的问题. 首先在  ...

  6. Java 学习笔记之 JVM初识

    JVM初识: java只是启动JVM的命令.JVM真实位置: C:\Program Files\Java\jdk1.8.0_121\jre\bin\server\jvm.dll 1. 第一行JDK版本 ...

  7. 【CSS】width和height计算

    width:calc(100% - 20px); width:-webkit-calc(100% - 20px);//chrome width:-moz-calc(100% - 20px);//fir ...

  8. MySQL 和 Navicat Premium 下载及安装全过程

    前言: 我对 “MySQL社区版” 的理解是:它只是一个后台服务,它的管理需要用到其他的数据库管理软件,这里我用的是 Navicat Premium,这个软件可以同时为多个数据库提供管理,比如MySQ ...

  9. UWP开发入门(二十四)—— Win10风格的打印对话框

    虽然经常看到阿迪王发“看那个开发UWP的又上吊了”的图……还是忍不住重启一下这个系列.最近有用到UWP的print API,特地来写一篇给某软的这个伟大构想续一秒. 之前的打印对话框差不多长成这样: ...

  10. Java求和的整体思路

    一.    设计思想: 设计这个程序我们需要考虑到参数的输入,并且可以输入多个参数,以及为用户考虑到各种的边界问题.首先第一步我们应该给出输入参数的语句,让用户可以输入.第二步我们应对其进行参数个数的 ...