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

栈的压入、弹出序列

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

import java.util.*;

public class Solution {
    public boolean IsPopOrder(int [] pushA,int [] popA) {
      Stack<Integer> st = new Stack<>();

      int index = 0;
      for(int i = 0; i < pushA.length; ++i){
          st.push(pushA[i]);
          while(!st.isEmpty() && index <= popA.length && st.peek() == popA[index]){
              st.pop();
              index++;
          }
      }

      return st.isEmpty();
    }
}
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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