ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

剑指 Offer 31. 栈的压入、弹出序列

2021-03-31 21:35:00  阅读:171  来源: 互联网

标签:popped return 压入 Offer 31 pushed length && 序列


题目:

输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如,序列 {1,2,3,4,5} 是某栈的压栈序列,序列 {4,5,3,2,1} 是该压栈序列对应的一个弹出序列,但 {4,3,5,1,2} 就不可能是该压栈序列的弹出序列。

 

示例 1:

输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
输出:true
解释:我们可以按以下顺序执行:
push(1), push(2), push(3), push(4), pop() -> 4,
push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

示例 2:

输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
输出:false
解释:1 不能在 2 之前弹出。

 

提示:

  1. 0 <= pushed.length == popped.length <= 1000
  2. 0 <= pushed[i], popped[i] < 1000
  3. pushed 是 popped 的排列。

代码:

 1 class Solution {
 2     public boolean validateStackSequences(int[] pushed, int[] popped) {
 3         //长度不一样
 4         if(pushed.length!=popped.length){return false;}  
 5         //有一个为空
 6         if((pushed.length==0&&popped.length!=0)||(pushed.length!=0&&popped.length==0)){return false;}
 7         //两个都为0
 8          if((pushed.length==0&&popped.length==0)){return true;}
 9         //处理正常数组
10         List<Integer> list=new ArrayList<>();
11         for(int i=0;i<pushed.length;i++){
12             list.add(pushed[i]);
13         }
14         int i=0;
15         int j=0;
16         while(i<list.size()&&j<popped.length){
17            //退出条件,j比较完,或者i大于list集合的长度
18             if(j>=popped.length||i>list.size()){break;}           
19             else if(list.get(i)==popped[j]){
20                 list.remove(i);
21                 j++;               
22                 if(i>0){i--;}   //处理下标i不能小于0
23 
24             }
25             else{
26                 i++;
27             }
28 
29         }
30         if(j>=popped.length){return true;}
31         else{return false;}
32 
33     }
34 }

 

 

 

 1 class Solution {
 2     public boolean validateStackSequences(int[] pushed, int[] popped) {
 3         //长度不一样
 4         if(pushed.length!=popped.length){return false;}  
 5         //有一个为空
 6         if((pushed.length==0&&popped.length!=0)||(pushed.length!=0&&popped.length==0)){return false;}
 7         //两个都为0
 8          if((pushed.length==0&&popped.length==0)){return true;}
 9         //处理正常数组
10         
11         Stack<Integer> stack =new Stack<>();
12         int i=0;
13         for(int num : pushed){
14             stack.push(num);
15             while(!stack.isEmpty()&&stack.peek()==popped[i]){
16                 stack.pop();
17                 ++i;
18             }
19         }
20     
21          if(i>=popped.length){return true;}
22         return false;
23     }
24 }

 

标签:popped,return,压入,Offer,31,pushed,length,&&,序列
来源: https://www.cnblogs.com/SEU-ZCY/p/14603876.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有