当前位置:网站首页>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();
}
边栏推荐
- AUTOSAR从入门到精通100讲(106)-域控制器中的SOA
- Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- Exercise 7-2 finding the maximum value and its subscript (20 points)
- Regular expression (I)
- Kotlin set operation summary
- Batch distribution of SSH keys and batch execution of ansible
- 【OpenCV 例程200篇】218. 多行倾斜文字水印
- A little feeling
- Realsense d435 d435i d415 depth camera obtains RGB map, left and right infrared camera map, depth map and IMU data under ROS
猜你喜欢
回复评论的sql
Hands on deep learning (36) -- language model and data set
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Web端自动化测试失败原因汇总
leetcode1-3
Pcl:: fromrosmsg alarm failed to find match for field 'intensity'
IIS configure FTP website
Nuxt reports an error: render function or template not defined in component: anonymous
Mmclassification annotation file generation
Summary of reasons for web side automation test failure
随机推荐
Does any teacher know how to inherit richsourcefunction custom reading Mysql to do increment?
Dynamic memory management
Reprint: summation formula of proportional series and its derivation process
百度研发三面惨遭滑铁卢:面试官一套组合拳让我当场懵逼
按键精灵打怪学习-识别所在地图、跑图、进入帮派识别NPC
查看CSDN个人资源下载明细
System.currentTimeMillis() 和 System.nanoTime() 哪个更快?别用错了!
Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
Batch distribution of SSH keys and batch execution of ansible
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
JDBC and MySQL database
【Day1】 deep-learning-basics
入职中国平安三周年的一些总结
Mmclassification annotation file generation
Basic principle of servlet and application of common API methods
Latex insert picture, insert formula
基于线性函数近似的安全强化学习 Safe RL with Linear Function Approximation 翻译 2
Development guidance document of CMDB
Custom type: structure, enumeration, union
7-17 crawling worms (15 points)