zoukankan      html  css  js  c++  java
  • 栈的压入、弹出序列

     1 package com.algorithm;
     2 
     3 import java.util.Stack;
     4 
     5 /*输入两个整数序列,第一个序列表示栈的压入顺序,
     6 请判断第二个序列是否为该栈的弹出顺序。假设压入
     7 栈的所有数字均不相等。例如序列1,2,3,4,5是某
     8 栈的压入顺序,序列4,5,3,2,1是该压栈序列对
     9 应的一个弹出序列,但4,3,5,1,2就不可能是该压
    10 栈序列的弹出序列。(注意:这两个序列的长度是相等的)*/
    11 public class AdjustIsPopOrder {
    12      public boolean IsPopOrder(int [] pushA,int [] popA) {
    13         if(pushA.length == 0 || popA.length == 0)
    14             return false;
    15         Stack<Integer> s = new Stack<Integer>();//辅助栈,存储入栈
    16         int indexofPopA = 0;//辅助指针,记录popA栈的位置
    17         for(int i = 0; i < pushA.length; i++){
    18             s.push(pushA[i]);
    19             while(!s.empty() && s.peek() == popA[indexofPopA]){
    20                 s.pop();
    21                 indexofPopA++;
    22             }
    23         }
    24         return s.empty();
    25      }
    26 }
  • 相关阅读:
    278.First Bad Version
    277. Find the Celebrity
    256.Paint House
    276. Paint Fence
    275. H-Index II
    274. H-Index
    273. Integer to English Words
    272. Closest Binary Search Tree Value II
    270. Closest Binary Search Tree Value
    271. Encode and Decode Strings
  • 原文地址:https://www.cnblogs.com/fankongkong/p/6529529.html
Copyright © 2011-2022 走看看