I. Title Description:

Leetcode-cn.com/problems/va…

Given two sequences of pushed and popped, the values in each sequence are not repeated, returning true only if they may be the result of a sequence of push and pop operations on the originally empty stack; Otherwise, return false.

**** Example 1:

Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1]  push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1Copy the code

Ii. Analysis of Ideas:

  • I’m just going to simulate the whole stack order, and see if the stack length is equal to zero

Iii. AC Code:

/** * @param {number[]} pushed * @param {number[]} popped * @return {boolean} */ var validateStackSequences = function(pushed, popped) { const stack = [] for(let i=0; i<pushed.length; i++){ stack.push(pushed[i]) while(popped.length && popped[0] === stack[stack.length-1]){ popped.shift() stack.pop() } } return stack.length === 0 };Copy the code

Iv. Summary:

This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign