当前位置:网站首页>5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)

5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)

2022-06-26 04:54:00 Caicai's big data development path

lt.946. Verify stack sequence

[ Case needs ]

 Insert picture description here

[ Thought analysis ]

 Insert picture description here

[ Code implementation ]

class Solution {
    
    public boolean validateStackSequences(int[] pushed, int[] popped) {
    
        //pushed First out of the stack ,  encounter poped The same elements of ,  Then take it. pop Full stack ;
        Deque<Integer> stack = new LinkedList<>();
        int i = 0;

        for(int num : pushed){
    
            stack.push(num);        

            while(!stack.isEmpty() && stack.peek() == popped[i]){
    
                stack.pop();
                ++i;
            }
        }

        return stack.isEmpty();
    }
}

The specific problem solution : Am I
 Insert picture description here

原网站

版权声明
本文为[Caicai's big data development path]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260449270984.html