题解 | #栈的压入、弹出序列#

栈的压入、弹出序列

http://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106

用一个栈模拟一下就好了

import java.util.*;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
      Stack<Integer> we = new Stack<>();
        int j=0;
      for(int i =0;i<pushA.length;i++){
        
              we.push(pushA[i]);
          
          
          while(true){
              if(we.peek() == popA[j]){
                  we.pop();
                  j++;
                  if(j == popA.length || we.isEmpty()){
                      break;
                  }
              }
              else if(we.peek() != popA[j]){
                  break;
              }
          }
      }
        if(we.isEmpty()){
            return true;
         }
        else{return false;}
    }
}
全部评论

相关推荐

06-07 19:59
门头沟学院 C++
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务