当前位置:网站首页>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;
}
}
}边栏推荐
猜你喜欢
随机推荐
async和await用法介绍
【ACWing】406. 放置机器人
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
08-SDRAM:汇总
Enterprise firewall management, what firewall management tools are there?
Flink Yarn Per Job - CliFrontend
els 方块变形判断。
security CSRF漏洞保护
【图像融合】基于加权和金字塔实现图像融合附matlab代码
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
学习英语的网站与资料
Excel表格数据导入MySQL数据库
控制电机的几种控制电路原理图
20220725 Information update
PHP从txt文件中读取数据的方法
如何设计循环队列?快进来学习~
ES中SQL查询详解
DVWA靶场环境搭建
Cash Ⅱ LeetCode_518_ change
DOM 事件及事件委托
![[头条]笔试题——最小栈](/img/67/08f2be8afc780e3848371a1b5e04db.png)








