当前位置:网站首页>Sword finger offer: push in and pop-up sequence of stack
Sword finger offer: push in and pop-up sequence of stack
2022-04-23 04:47:00 【wyplj_ sir】
subject
Enter two sequences of integers , The first sequence represents the push order of the stack , Determine whether the second sequence is the pop-up order for the stack . Suppose that all the Numbers pushed are not equal . for example , Sequence {1,2,3,4,5} Is the stack pressing sequence of a stack , Sequence {4,5,3,2,1} Is a popup sequence corresponding to the stack sequence , but {4,3,5,1,2} It cannot be a popup sequence of the push sequence .
Example
Input :pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output :true
explain : We can do it in the following order :
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
Input :pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output :false
explain :1 Can't be in 2 Pop up before .
Code
Borrow an auxiliary stack stackstack , simulation Push the / Arrangement of pop-up operations . Depending on whether the simulation is successful , You get the result .
Into the stack, : Execute in the order of stack pressing sequence .
The stack, : After each stack , Circular judgement “ Top element of stack == Pop up the current element of the sequence ” Is it true , Pop up all stack top elements that meet the pop-up sequence order .
Answer key
class Solution {
public boolean validateStackSequences(int[] pushed, int[] popped) {
if(pushed == null && popped == null){
return true;
}
if(pushed == null || popped == null){
return false;
}
if(pushed.length == 0 && popped.length == 0){
return true;
}
if(pushed.length != popped.length){
return false;
}
LinkedList<Integer> stack = new LinkedList<>();
int index = 0;
for(int i=0;i<pushed.length;i++){
stack.push(pushed[i]);
while(index < popped.length && !stack.isEmpty() && popped[index] == stack.peek()){
stack.pop();
index++;
}
}
return stack.isEmpty();
}
}
版权声明
本文为[wyplj_ sir]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220555179095.html
边栏推荐
- 383. Ransom letter
- Use model load_ state_ Attributeerror appears when dict(): 'STR' object has no attribute 'copy‘
- Unity摄像头跟随鼠标旋转
- Innovation training (V) mid term inspection
- leetcode008--实现strStr()函数
- Introduction to raspberry pie 3B - system installation
- leetcode007--判断字符串中的括号是否匹配
- L2-011 玩转二叉树(建树+BFS)
- redis和mysql区别
- Progress of innovation training (III)
猜你喜欢
test
Druid -- JDBC tool class case
Pixel mobile phone brick rescue tutorial
win10, mysql-8.0.26-winx64. Zip installation
View analysis of scenic spots in ArcGIS
Improving 3D object detection with channel wise transformer
Go reflection rule
Spark FAQ sorting - must see before interview
拼了!两所A级大学,六所B级大学,纷纷撤销软件工程硕士点!
test
随机推荐
Leetcode001 -- returns the subscript of the array element whose sum is target
数据孤岛是什么?为什么2022年仍然存在数据孤岛?
/etc/bash_ completion. D directory function (the user logs in and executes the script under the directory immediately)
JS generates a specified number of characters according to some words
Error occurs when thymeleaf th: value is null
Key points of AWS eks deployment and differences between console and eksctl creation
What is a blocking queue? What is the implementation principle of blocking queue? How to use blocking queue to implement producer consumer model?
Leetcode003 -- judge whether an integer is a palindrome number
Recursive call -- Enumeration of permutations
io. Platform. packageRoot; // ignore: deprecated_ Member_ use
Leetcode009 -- search the target value in the array with binary search
js 判断数字字符串中是否含有字符
Simply drag objects to the item bar
getprop 属性
Manually write smart pointer shared_ PTR function
Custom switch control
win10, mysql-8.0.26-winx64. Zip installation
Unity3d practical skills - theoretical knowledge base (I)
解决ValueError: Argument must be a dense tensor: 0 - got shape [198602], but wanted [198602, 16].
Leetcode - > 1 sum of two numbers