155. Min Stack

class MinStack {
int min = Integer.MAX_VALUE;
Stack<Integer> stack = new Stack<Integer>();
/** initialize your data structure here. */
public MinStack() { } public void push(int x) {
if(x <= min){
stack.push(min);
min = x;
}
stack.push(x);
} public void pop() {
if(stack.pop() == min) min = stack.pop();
} public int top() {
return stack.peek();
} public int getMin() {
return min;
}
}

232. Implement Queue using Stacks

用一个input和outlput栈,当pop时,output为空,则把input全部压入output中。

class MyQueue {
Stack<Integer> input = new Stack();
Stack<Integer> output = new Stack();
/** Initialize your data structure here. */
public MyQueue() { } /** Push element x to the back of queue. */
public void push(int x) {
input.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
peek();
return output.pop();
} /** Get the front element. */
public int peek() {
if(output.empty())
while(!input.empty())
output.push(input.pop());
return output.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
return input.empty() && output.empty();
}
}

225. Implement Stack using Queues

在push的时候就直接把顺序缓过来

class MyStack {
Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
this.queue = new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
queue.add(x);
for(int i = 0; i < queue.size() - 1; i++){
queue.add(queue.poll());
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
} /** Get the top element. */
public int top() {
return queue.peek();
} /** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}

11/9 <Stack> 155 232 225的更多相关文章

  1. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  2. D3_book 11.2 stack

    <!-- book :interactive data visualization for the web 11.2 stack 一个堆叠图的例子 --> <!DOCTYPE htm ...

  3. tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask

    1.  tf.split(3, group, input)  # 拆分函数    3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...

  4. leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues

    155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...

  5. 第11天 Stack Queue

    1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...

  6. 数据结构11: 栈(Stack)的概念和应用及C语言实现

    栈,线性表的一种特殊的存储结构.与学习过的线性表的不同之处在于栈只能从表的固定一端对数据进行插入和删除操作,另一端是封死的. 图1 栈结构示意图 由于栈只有一边开口存取数据,称开口的那一端为“栈顶”, ...

  7. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  8. 11月26号host

    127.0.0.1 localhost255.255.255.255 broadcasthost::1 localhostfe80::1%lo0 localhost # Google start216 ...

  9. 11月16host文件

    #################################################################################################### ...

随机推荐

  1. 一文读懂前端技术演进:盘点Web前端20年的技术变迁史

    本文原文由作者“司徒正美”发布于公众号“前端你别闹”,即时通讯网收录时有改动,感谢原作者的分享. 1.引言 1990 年,第一个Web浏览器的诞生:1991 年,WWW诞生,这标志着前端技术的开始. ...

  2. docker入门与部署微服务--学习笔记

    最近公司进一步去windows,走向 linux+云化. 原来的一大坨windows虚拟机服务器都要转向linux, 既然走向linux的话,那么docker肯定是要涉足的. 故学习了docker入门 ...

  3. redis之HyperLogLog

    HyperLogLog 提供不精确的去重计数方案,虽然不精确但是也不是非常不精确,标准误差是 0.81%. 使用方法 HyperLogLog 提供了两个指令 pfadd 和 pfcount,根据字面意 ...

  4. thymeleaf入门

    controller层添加实体 html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> ...

  5. 嵌入式Linux+NetCore 笔记一

    记录嵌入式Linux+NetCore培训中遇到的一些问题以及解决方法 十一放假期间发现园里大神大石头(NewLife团队)开了一个嵌入式Linux+NetCore培训,就报名参加了.更幸运的是,我刚好 ...

  6. Microsoft.Windows.Controls.Ribbon.RibbonWindow 碰到 AvalonDock出现的诡异现象

    部分一 14年底进入目前公司时,领导准备开发一款新软件平台以取代原有平台.原平台采用C++Build开发界面(window c/s客户端) .Visual Studio(封装dll模块).过完年,领导 ...

  7. WPF-数据模板深入(加载XML类型数据)

    一.我们知道WPF数据模板是当我们给定一个数据类型,我们为这个数据类型写好布局,就给这种数据类型穿上了外衣. 下面这个例子,能够帮助大家充分理解数据模板就是数据类型的外衣的意思:(里面的MyListB ...

  8. 纯C语言实现顺序栈

    #include <stdio.h> #include <stdlib.h> #define MAXSIZE 100 typedef int SElemType; typede ...

  9. js中this由来

    这篇文章主要是讲述js中的this是什么?是怎么来的? 我们首先创建一个person对象,如下: var person = { name: 'wyh', age: 22, sayHi: functio ...

  10. js删除html标记 去掉所有html标记

    js删除html标记 去掉所有html标记 function delHtml(str){ return str.replace(/<[^>]+>/g,""); / ...