当前位置:网站首页>[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();}}
边栏推荐
- Is TCP reliable?Why?
- Using the "stack" fast computing -- reverse polish expression
- TCP 可靠吗?为什么?
- 不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
- Zadig 面向开发者的自测联调子环境技术方案详解
- 06-SDRAM :SDRAM控制模块
- windows sql server 如何卸载干净?
- Unity—四元数、欧拉角API+坐标系统
- @Transactional 注解使用详解
- How to solve the error when mysql8 installs make
猜你喜欢
随机推荐
多御安全浏览器android版更新至1.7,改进加密协议
双队列实现栈?双栈实现队列?
Chrome书签插件,让你实现高效整理
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
2022还想上岸学习软件测试必看,测试老鸟的肺腑之言...
【MySQL系列】 MySQL表的增删改查(进阶)
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
【图像融合】基于加权和金字塔实现图像融合附matlab代码
ES中SQL查询详解
security CSRF漏洞保护
短视频SEO搜索运营获客系统功能介绍
@WebServlet注解(Servlet注解)
[头条]笔试题——最小栈
yay 报错 response decoding failed: invalid character ‘<‘ looking for beginning of value;
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
How to solve the error when mysql8 installs make
【ACWing】406. 放置机器人
短视频seo搜索优化主要内容
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
NFT工具合集