Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【挑战系列6】用JS实现判断堆栈的出栈顺序是否合理 #8

Open
Chocolate1999 opened this issue Aug 31, 2020 · 0 comments

Comments

@Chocolate1999
Copy link
Owner

/* 判断堆栈的出栈顺序是否合理 */
let validStack = (inArr, outArr) => {
  if(inArr == null || outArr == null) return false
  let stack = []
  for(let i=0;i<inArr.length;i++){
    stack.push(inArr[i])
    while(stack.length && stack[stack.length-1] === outArr[0]){
      stack.pop()
      outArr.shift()
    }
  }
  return stack.length === 0? true : false
}
let inArr = [1,2,3,4,5]
let outArr = [4,5,3,2,1]
console.log(validStack(inArr, outArr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant