11/9 <Stack> 155 232 225
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的更多相关文章
- 【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( ...
- D3_book 11.2 stack
<!-- book :interactive data visualization for the web 11.2 stack 一个堆叠图的例子 --> <!DOCTYPE htm ...
- 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 ...
- 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 ...
- 第11天 Stack Queue
1.Stack package algs4; import java.util.Iterator; import java.util.NoSuchElementException; public cl ...
- 数据结构11: 栈(Stack)的概念和应用及C语言实现
栈,线性表的一种特殊的存储结构.与学习过的线性表的不同之处在于栈只能从表的固定一端对数据进行插入和删除操作,另一端是封死的. 图1 栈结构示意图 由于栈只有一边开口存取数据,称开口的那一端为“栈顶”, ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 11月26号host
127.0.0.1 localhost255.255.255.255 broadcasthost::1 localhostfe80::1%lo0 localhost # Google start216 ...
- 11月16host文件
#################################################################################################### ...
随机推荐
- Java网络传输数据加密算法
算法可逆,具有跨平台特性 import java.io.IOException; import java.io.UnsupportedEncodingException; import java.se ...
- Azure EA (3) 使用Postman访问海外Azure Billing API
<Windows Azure Platform 系列文章目录> 本文介绍的是海外版的Azure Global服务,因为跨境内境外网络,访问速度会比较慢 在开始使用Azure Billing ...
- 自动化API之一 自动生成Mysql数据库的微服务API
本文演示如何利用Uniconnector平台,自动生成Mysql数据库的API,节约开发人员编写后台API的时间.使用生成API的前提是开发者有 自己的数据库,有数据库的管理权限,并能通过外网 ...
- Pytest 使用简介
前言 最近在听极客时间的课程,里面的讲师极力推崇 pytest 框架,鄙视 unittest 框架,哈哈!然后查了些资料,发现了一条 python 鄙视链:pytest 鄙视 > unittes ...
- Mac终端常用快捷键
Ctrl + a 跳到行首Ctrl + e 跳到行尾Ctrl + d 删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof)Ctrl + h ...
- LuoguP5290 [十二省联考2019]春节十二响 | 启发式合并
还有33天就要高考了,我在干啥-- 题目概述 一棵有根树,每个节点有权值. 要求把所有节点分成组,具有祖先-后代关系的两个节点不能被分到同一组. 每一组的代价是所包含的节点的最大权值,最小化所有组的代 ...
- appstore-react v2.0—redux-actions和redux-saga的应用
开发文档 https://redux-saga.js.org/ https://redux-saga-in-chinese.js.org/ https://redux-actions.js.org/ ...
- 自动轮播swiper css实现
@keyframes scale { 0% { transform: scale(1, 1); opacity: 0.5; z-index:; transition: opacity z-index ...
- Linux(CentOS)启动时自动执行脚本(rc.local)
下面说说通过rc.local文件进行开机启动 1.首先创建一个启动脚本,这里以启动docker为例 创建 docker-startup.sh 脚本 #! /bin/bash /usr/bin/mk-d ...
- IDEA的常用配置(Maven)一键导入及优化内存
IDEA的常用配置一键导入 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载如图的压缩包 下载完成后解压缩,点击settings_bak,你会看 ...