当前位置:网站首页>Sword finger offer 31 Stack push in and pop-up sequence
Sword finger offer 31 Stack push in and pop-up sequence
2022-07-04 10:16:00 【JoesonChan】
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 1:
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
Example 2:
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 .
Tips :
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed yes popped Permutation .
public static boolean isStackArray(int[] nums1, int[] nums2) {
if (nums1.length != nums2.length) {
return false;
}
int index1 = nums1.length - 1;
LinkedList<Integer> stack = new LinkedList<>();
for (int index2 = 0; index2 < nums2.length; index2++) {
stack.push(nums2[index2]);
while (!stack.isEmpty() && nums1[index1] == stack.peek()) {
index1--;
stack.pop();
}
}
return stack.isEmpty();
}
边栏推荐
- Use C to extract all text in PDF files (support.Net core)
- A little feeling
- 基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- Go context 基本介绍
- How can people not love the amazing design of XXL job
- 【OpenCV 例程200篇】218. 多行倾斜文字水印
- Regular expression (I)
- Reasons and solutions for the 8-hour difference in mongodb data date display
- Kotlin: collection use
猜你喜欢
How web pages interact with applets
Kubernetes CNI 插件之Fabric
Hands on deep learning (33) -- style transfer
Dynamic memory management
Hands on deep learning (46) -- attention mechanism
Sort out the power node, Mr. Wang he's SSM integration steps
Hands on deep learning (32) -- fully connected convolutional neural network FCN
Regular expression (I)
Safety reinforcement learning based on linear function approximation safe RL with linear function approximation translation 1
Hands on deep learning (38) -- realize RNN from scratch
随机推荐
Fabric of kubernetes CNI plug-in
Hands on deep learning (37) -- cyclic neural network
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
Hands on deep learning (39) -- gating cycle unit Gru
2021-08-11 function pointer
Sort out the power node, Mr. Wang he's SSM integration steps
Go context basic introduction
Exercise 9-3 plane vector addition (15 points)
Histogram equalization
Golang 类型比较
Write a mobile date selector component by yourself
对于程序员来说,伤害力度最大的话。。。
Hands on deep learning (41) -- Deep recurrent neural network (deep RNN)
用数据告诉你高考最难的省份是哪里!
Work order management system OTRs
六月份阶段性大总结之Doris/Clickhouse/Hudi一网打尽
Whether a person is reliable or not, closed loop is very important
C # use gdi+ to add text with center rotation (arbitrary angle)
JDBC and MySQL database
H5 audio tag custom style modification and adding playback control events