首页 技术 正文
技术 2022年11月17日
0 收藏 493 点赞 5,123 浏览 2376 个字

[抄题]:

实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值。

你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成。

[思维问题]:

想不到,脑洞不够大

[一句话思路]:

用一个minstack来辅助实现

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 主函数中,数据结构为类+名 类是Stack<Integer>
  2. minStack.empty() == true 或者isempty都可以
  3. pop分为两种情况:二者peek(不是peak)相等的、不相等的; 方法加点来用

[二刷]:

  1. push可能有空的情况,要注意

[总结]:top只是看一看,peek也只是看一看。所以top里面包含peek

[复杂度]:Time complexity: O(1) Space complexity: O(n)

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

public class MinStack {
private Stack<Integer> stack;
private Stack<Integer> minStack; public MinStack() {
// do intialization if necessary
stack = new Stack<Integer>();
minStack = new Stack<Integer>();
} /*
* @param number: An integer
* @return: nothing
*/
public void push(int number) {
// write your code here
stack.push(number);
if (minStack.isEmpty()) {
minStack.push(number);
}
else {
if (number <= minStack.peek()) {
minStack.push(number);
}
}
} /*
* @return: An integer
*/
public int pop() {
// write your code here
if (stack.peek().equals(minStack.peek())) {
minStack.pop();
}
return stack.pop();
} /*
* @return: An integer
*/
public int min() {
// write your code here
return minStack.peek();
}
}
class MinStack {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>(); /** initialize your data structure here. */
public MinStack() {
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> minStack = new Stack<Integer>();
} public void push(int x) {
//stack: push
stack.push(x);//s:2 //min stack:push when it's null or minimum
if (minStack.isEmpty()) {
minStack.push(x);
} else if (x <= minStack.peek()) {
minStack.push(x); //ms:2
}
} public void pop() {
//stack:get, min stack: pop when it's mean
if (minStack.peek().equals(stack.peek())) {
minStack.pop(); //both 2, ms pop 2
}
stack.pop(); //s pop 2
} public int top() {
return stack.peek(); //return s pop 2
} public int getMin() {
return minStack.peek(); //return ms pop 2
}
}/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/

解法2: 只用1个stack

[抄题]:

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

不懂题目意思:设计新的数据结构。原生的stack中本来就有push pop peek方法

[一句话思路]:

刷新最小值时 stack中存2个 留着最小值备胎。否则只存1个 要走就走,下面一层还是最小值。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

带最小值操作的栈 · Min Stack

[一刷]:

  1. pop一个元素之后把备胎也pop出去,以便于下次的pop, 不留痕迹
  2. class中声明后,MinStack()方法用于建立新的数据类型(真心是没话可说了)

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

备胎需要斩草除根

[复杂度]:Time complexity: O(1) Space complexity: O(n)

都是立存立取的

[英文数据结构或算法,为什么不用别的数据结构或算法]:

原生的stack中本来就有push pop peek方法

[关键模板化代码]:

[其他解法]:

2个stack

[Follow Up]:

[LC给出的题目变变变]:

716. Max Stack 2个stack

[代码风格] :

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919