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 }

 

猜你喜欢

转载自www.cnblogs.com/sheepcore/p/12395423.html
今日推荐