LeetCode-946 Validate Stack Sequences Solution (with Java)

1. Description:

Notes:

2. Examples:

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-05-09
 3  */
 4 class Solution {
 5     public boolean validateStackSequences(int[] pushed, int[] popped) {
 6         Stack<Integer> stack = new Stack<>();
 7         int i = 0, j = 0;
 8         while (i < pushed.length && j < popped.length){
 9             stack.push(pushed[i++]);
10             while(!stack.isEmpty() && stack.peek() == popped[j]){
11                 stack.pop();
12                 j++;
13             }
14         }
15         return stack.isEmpty();
16     }
17 }

 

 

posted @ 2020-03-02 14:17  SheepCore  阅读(167)  评论(0编辑  收藏  举报