Array지만 stack처럼 취급할 stack
변수를 선언했다.
pushed를 돌면서 stack
에 더해주었다.
pop해야 하는 수와 현재 stack의 가장 마지막이 같다면 pop할 수 있다.
func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool {
var stack: [Int] = [], index = 0
for push in pushed {
stack.append(push)
while index < popped.count && popped[index] == stack.last {
stack.popLast()
index += 1
}
}
return stack.isEmpty
}