LeetCode prove safety offer Q31 pressed stack, pop-up sequence

Thinking

By using an auxiliary stack array to simulate pushed into the process stack. When each element pushed onto the stack, using a while loop and determines whether the current stack poped same, if the same for both pop.
Finally, if the stack is empty, it is correct or wrong.

Code

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
     //Stack<Integer> s=new Stack<>();
     int[] st=new int[1001];
     int end=-1;
     int len=pushed.length;
     if(len==0) return true;
     int j=0;
     for(int i=0;i<len;i++){
         st[++end]=pushed[i];
         //s.add(pushed[i]);
         while(end!=-1&&j<len&&popped[j]==st[end]){
             //s.pop();
             end--;
             j++;
         }
     }
     return end<0;
    }
}
Published 42 original articles · won praise 0 · Views 1945

Guess you like

Origin blog.csdn.net/c630565685/article/details/104802069