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文件
#################################################################################################### ...
随机推荐
- 公式推导【IoUNet//ECCV2018】
Jiang B, Luo R, Mao J, Xiao T, Jiang Y. Acquisition of localization confidence for accurate object d ...
- Unity开发实战探讨-资源的加载释放最佳策略简要心得
Unity开发实战探讨-资源的加载释放最佳策略简要心得 看过我另外一篇关于Unity资源释放随笔<Unity开发实战探讨-资源的加载释放最佳策略>如果觉得略微复杂,那么下面是一些比较简要的 ...
- svn merge操作
使用SVN做Merge操作时,会包含6个选项,下面就这6个选项给出详细的说明: 1.Merge a range of revisions 此类型应用最为广泛,主要是把源分支中的修改合并到目标分支上来. ...
- (三十二)golang--面向对象之封装
封装:把抽象出来的字段和对字段的操作封装在一起,数据被保护在内部,程序的其它包只有通过被授权的操作(方法),才能对字段进行操作. 封装的好处: (1)隐藏实际的细节: (2)可以对数据进行验证,保证安 ...
- CodeForces 463D DP
Gargari got bored to play with the bishops and now, after solving the problem about them, he is tryi ...
- 『The Counting Problem 数位dp』
The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...
- Winform 通过 WebBrowser 与 JS 交互
Winform 通过 WebBrowser 与 JS 交互 魏刘宏 2019.08.17 之前在使用 Cef (可在 Winform 或 WPF 程序中嵌入 Chrome 内核的网页浏览器的组件)时, ...
- 腾讯WeTest&TesterHome深圳线下沙龙
腾讯官方的一站式品质开放平台「腾讯WeTest」携手知名测试社区「TesterHome」以及3家金融相关企业为我们带来的金融专场. 本次活动内容主要以金融公司的测试落地和测试技术为主,我们希望你是一个 ...
- wpf 窗体添加背景图片
方法一:xaml中:<控件> <控件.Background><ImageBrush ImageSource="/WpfApplication1;compon ...
- Python odoo中嵌入html简单的分页功能
在odoo中,通过iframe嵌入 html,页面数据则通过controllers获取,使用jinja2模板传值渲染 html页面分页内容,这里写了判断逻辑 <!-- 分页 -->< ...