当前位置:网站首页>[Headline] Written test questions - minimum stack
[Headline] Written test questions - minimum stack
2022-08-02 00:15:00 【Chen Yikang】


Solution 1: (relative to solution 2, it will be very tasteless)
Thinking:
- Because the time complexity of obtaining the minimum element of the stack is O(1), all use a variable min to store the minimum value
- Set the variable min that stores the minimum value to Integer.MAX_VALUE
- In the push method, use min to compare each push
- Create two stacks, the first stack is used to store data, and the second stack is used to obtain the zero-hour stack with the minimum data through the first stack of variables
class MinStack {public Stack stack;public Stack stackTmp;public MinStack() {stack = new Stack<>();stackTmp = new Stack<>();}// first assign the maximum valuepublic int min = Integer.MAX_VALUE;public void push(int val) {// Assign a value if it is smaller than himif(val < min){min = val;}stack.push(val);}public void pop() {if(stack.pop() == min){//min needs to be reassigned after being popped, still using the maximum valuemin = Integer.MAX_VALUE;//Transfer all elements to stackTmp, the purpose is to make min re-comparison with all elementswhile(!stack.empty()){int tmp = stack.pop();if(tmp < min){min = tmp;}stackTmp.push(tmp);}// then move to stackwhile(!stackTmp.empty()){stack.push(stackTmp.pop());}}}public int top() {return stack.peek();}public int getMin() {return min;}} Solution 2: (analysis is as follows)





The code is as follows:
class MinStack {Stack stack;//Normal stackStack stackMin;//Minimum stackpublic MinStack() {stack = new Stack<>();stackMin = new Stack<>();}// push stackpublic void push(int val) {//Assign the initial value to the minimum stationif(stackMin.empty()){stackMin.push(val);}//If it is not the initial value, first judge whether it is less than or equal to (to prevent it from being empty after pop) the top element of the minimum stack, if so, push the stack to the minimum stackelse if(stackMin.peek() >= val){stackMin.push(val);}stack.push(val);}public void pop() {if((int)stackMin.peek() == (int)stack.peek()){stackMin.pop();}stack.pop();}public int top() {return stack.peek();}public int getMin() {return stackMin.peek();}} 边栏推荐
- 协作乐高 All In One:DAO工具大全
- 【ACWing】230. 排列计数
- Excel表格数据导入MySQL数据库
- Ansible中的任务执行控制
- 如何优雅的消除系统重复代码
- Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
- Unity—四元数、欧拉角API+坐标系统
- 面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
- cdh6 opens oozieWeb page, Oozie web console is disabled.
- Architecture basic concept and nature of architecture
猜你喜欢
随机推荐
Axure tutorial - the new base (small white strongly recommended!!!)
How to get the best power efficiency in Windows 11?
Unity—四元数、欧拉角API+坐标系统
信息系统项目管理师必背核心考点(五十七)知识管理工具
els 长条变形
Thinkphp 5.0.24变量覆盖漏洞导致RCE分析
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
【Leetcode】479. Largest Palindrome Product
security cross-domain configuration
CDH6 Hue to open a "ASCII" codec can 't encode characters
SphereEx苗立尧:云原生架构下的Database Mesh研发实践
LeetCode_279_完全平方数
不就是个TCC分布式事务,有那么难吗?
Excel文件读写(创建与解析)
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
12306抢票,极限并发带来的思考?
An interesting project--Folder comparison tool (1)
在MySQL中使用MD5加密【入门体验】
go语言标准库fmt包怎么使用
如何进行数据库备份









