LeetCode-Algorithms-[Mid]面试题31. 栈的压入、弹出序列

面试题31. 栈的压入、弹出序列

LeetCode-Algorithms-[Mid]面试题31. 栈的压入、弹出序列_第1张图片

	public boolean validateStackSequences(int[] pushed, int[] popped) {
		Stack stack = new Stack();
		int j = 0, n = pushed.length, m = popped.length;
		for (int i = 0; i < n; ++i) {
			stack.add(pushed[i]);
			while (!stack.isEmpty() && j < m && stack.peek().equals(popped[j])) {
				stack.pop();
				j++;
			}
		}
		return stack.isEmpty();
	}

 

你可能感兴趣的:(LeetCode)