当前位置:网站首页>Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
2022-08-02 00:16:00 【Chen Yikang】
目录
热身:
1 . 若进栈序列为 1,2,3,4 ,进栈过程中可以出栈,则下列不可能的一个出栈序列是()
A: 1,4,3,2 B: 2,3,4,1 C: 3,1,4,2 D: 3,4,2,1
分析:There is an implicit condition here,压栈序列1,2,3,4,push the stack at that time,You can then pop out of the stack,简单来讲,让1进栈时,You can then1弹出栈,Push back on the stack;
如此分析,只有BOption is eligible:1进栈,2进栈,与BOption first pop sequence match,弹出2,Look at the push sequence again:1with the popup sequence3不匹配,Continue to compare the next element of the pushed sequence,如此往复,可得出答案.
JZ31栈的压入、弹出序列
思路:
- 创建一个栈stack
- The push sequence is pushed first and then compared with the pop sequence
- Pop up if equal
import java.util.*;
import java.util.ArrayList;
public class Solution {
public boolean IsPopOrder(int [] pushA,int [] popA) {
Stack<Integer> stack = new Stack<>();
int count = 0;//计数:Increment by one if it matches the pushed data
for(int i = 0; i < pushA.length; i++){
if(pushA[i] == popA[count]){
//先压栈,再判断
stack.push(pushA[i]);
while(stack.peek() == popA[count]){
stack.pop();
count++;
//popAThe instructions are correct after traversing
if(count == pushA.length){
return true;
}
/**
*弹出栈后,There may not be a single element left,这时再到while里判断,
*It is possible to dereference a null pointer
*/
if(stack.empty()){
break;
}
}
}
else{
stack.push(pushA[i]);
}
}
return false;
}
}
逆波兰表达式求值
The blogger has sorted out the blog,快去看看吧~
有效的括号
It is also in the understanding of the stack,Frequently asked questions
思路:
1.创建一个栈
2.Traverse the given array in the large direction
3.The small direction is compared to see if it matches
注意:Mismatch is divided into the following situations
a.左右括号不匹配
b.右括号多:当前下标是右括号,但栈为空
c.左括号多:The string array traversal is complete,But the stack is still not empty
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(ch == '(' || ch == '{' || ch == '['){
stack.push(ch);
}
else{
//说明ch是右括号
if(stack.empty()){
//右括号多
return false;
}
char tmp = stack.peek();
if(tmp == '(' && ch == ')' ||
tmp == '{' && ch == '}' ||
tmp == '[' && ch == ']'){
//The current parenthesis matches
stack.pop();
}
else{
//左右括号不匹配
return false;
}
}
}
//判断是否为空
if(stack.empty()){
return true;
}
else{
//左括号多
return false;
}
}
}
边栏推荐
- security cross-domain configuration
- 很多人喜欢用多御安全浏览器,竟是因为这些原因
- contentEditable属性
- go语言标准库fmt包怎么使用
- Using the "stack" fast computing -- reverse polish expression
- Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
- Study Notes: The Return of Machine Learning
- SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
- oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed
- IP核:FIFO
猜你喜欢
随机推荐
1个月写900多条用例,二线城市年薪33W+的测试经理能有多卷?
An interesting project--Folder comparison tool (1)
Docker实践经验:Docker 上部署 mysql8 主从复制
Is TCP reliable?Why?
利用“栈”快速计算——逆波兰表达式
如何优雅的消除系统重复代码
Using the "stack" fast computing -- reverse polish expression
06-SDRAM :SDRAM控制模块
【MySQL系列】MySQL数据库基础
async/await 原理及执行顺序分析
【ACWing】406. 放置机器人
Axure tutorial - the new base (small white strongly recommended!!!)
解析正则表达式的底层实现原理
Axure教程-新手入门基础(小白强烈推荐!!!)
工件SSMwar exploded 部署工件时出错。请参阅服务器日志了解详细信息
mysql8安装make报错如何解决
security跨域配置
els 方块变形
当奈飞的NFT忘记了Web2的业务安全
OpenCV DNN blogFromImage()详解