当前位置:网站首页>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();
}
边栏推荐
- Latex insert picture, insert formula
- A little feeling
- Basic principle of servlet and application of common API methods
- 2. Data type
- Today's sleep quality record 78 points
- 华为联机对战如何提升玩家匹配成功几率
- 2021-08-11 function pointer
- C语言指针面试题——第二弹
- Whether a person is reliable or not, closed loop is very important
- Button wizard business running learning - commodity quantity, price reminder, judgment Backpack
猜你喜欢
The time difference between the past time and the present time of uniapp processing, such as just, a few minutes ago, a few hours ago, a few months ago
智慧路灯杆水库区安全监测应用
直方图均衡化
Baidu R & D suffered Waterloo on three sides: I was stunned by the interviewer's set of combination punches on the spot
How can Huawei online match improve the success rate of player matching
C # use gdi+ to add text with center rotation (arbitrary angle)
C语言指针经典面试题——第一弹
Mmclassification annotation file generation
回复评论的sql
Hands on deep learning (45) -- bundle search
随机推荐
什么是 DevSecOps?2022 年的定义、流程、框架和最佳实践
Hands on deep learning (45) -- bundle search
Legion is a network penetration tool
Hands on deep learning (46) -- attention mechanism
Intelligent gateway helps improve industrial data acquisition and utilization
C # use ffmpeg for audio transcoding
System. Currenttimemillis() and system Nanotime (), which is faster? Don't use it wrong!
Container cloud notes
Realsense of d435i, d435, d415, t265_ Matching and installation of viewer environment
leetcode1-3
Golang 类型比较
SQL replying to comments
入职中国平安三周年的一些总结
Network disk installation
Nuxt reports an error: render function or template not defined in component: anonymous
Uniapp--- initial use of websocket (long link implementation)
Go context basic introduction
Exercise 9-1 time conversion (15 points)
How do microservices aggregate API documents? This wave of show~
Vs201 solution to failure to open source file HPP (or link library file)