返回介绍

solution / 0900-0999 / 0946.Validate Stack Sequences / README_EN

发布于 2024-06-17 01:03:32 字数 4722 浏览 0 评论 0 收藏 0

946. Validate Stack Sequences

中文文档

Description

Given two integer arrays pushed and popped each with distinct values, return true_ if this could have been the result of a sequence of push and pop operations on an initially empty stack, or _false_ otherwise._

 

Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]
Output: true
Explanation: We might do the following sequence:
push(1), push(2), push(3), push(4),
pop() -> 4,
push(5),
pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1

Example 2:

Input: pushed = [1,2,3,4,5], popped = [4,3,5,1,2]
Output: false
Explanation: 1 cannot be popped before 2.

 

Constraints:

  • 1 <= pushed.length <= 1000
  • 0 <= pushed[i] <= 1000
  • All the elements of pushed are unique.
  • popped.length == pushed.length
  • popped is a permutation of pushed.

Solutions

Solution 1

class Solution:
  def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
    j, stk = 0, []
    for v in pushed:
      stk.append(v)
      while stk and stk[-1] == popped[j]:
        stk.pop()
        j += 1
    return j == len(pushed)
class Solution {
  public boolean validateStackSequences(int[] pushed, int[] popped) {
    Deque<Integer> stk = new ArrayDeque<>();
    int j = 0;
    for (int v : pushed) {
      stk.push(v);
      while (!stk.isEmpty() && stk.peek() == popped[j]) {
        stk.pop();
        ++j;
      }
    }
    return j == pushed.length;
  }
}
class Solution {
public:
  bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
    stack<int> stk;
    int j = 0;
    for (int v : pushed) {
      stk.push(v);
      while (!stk.empty() && stk.top() == popped[j]) {
        stk.pop();
        ++j;
      }
    }
    return j == pushed.size();
  }
};
func validateStackSequences(pushed []int, popped []int) bool {
  stk := []int{}
  j := 0
  for _, v := range pushed {
    stk = append(stk, v)
    for len(stk) > 0 && stk[len(stk)-1] == popped[j] {
      stk = stk[:len(stk)-1]
      j++
    }
  }
  return j == len(pushed)
}
function validateStackSequences(pushed: number[], popped: number[]): boolean {
  const stk = [];
  let j = 0;
  for (const v of pushed) {
    stk.push(v);
    while (stk.length && stk[stk.length - 1] == popped[j]) {
      stk.pop();
      ++j;
    }
  }
  return j == pushed.length;
}
impl Solution {
  pub fn validate_stack_sequences(pushed: Vec<i32>, popped: Vec<i32>) -> bool {
    let mut stack = Vec::new();
    let mut i = 0;
    for &num in pushed.iter() {
      stack.push(num);
      while !stack.is_empty() && *stack.last().unwrap() == popped[i] {
        stack.pop();
        i += 1;
      }
    }
    stack.len() == 0
  }
}
/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
var validateStackSequences = function (pushed, popped) {
  let stk = [];
  let j = 0;
  for (const v of pushed) {
    stk.push(v);
    while (stk.length && stk[stk.length - 1] == popped[j]) {
      stk.pop();
      ++j;
    }
  }
  return j == pushed.length;
};
public class Solution {
  public bool ValidateStackSequences(int[] pushed, int[] popped) {
    Stack<int> stk = new Stack<int>();
    int j = 0;
    foreach (int x in pushed)
    {
      stk.Push(x);
      while (stk.Count != 0 && stk.Peek() == popped[j]) {
        stk.Pop();
        ++j;
      }
    }
    return stk.Count == 0;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文