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

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

	public boolean validateStackSequences(int[] pushed, int[] popped) {
		Stack<Integer> stack = new Stack<Integer>();
		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();
	}
发布了272 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/m0_37302219/article/details/105574143