[Swift]LeetCode946. 验证栈序列 | Validate Stack Sequences
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10015225.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given two sequences pushed
and popped
with distinct values, return true
if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack.
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.
Note:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
is a permutation ofpopped
.pushed
andpopped
have distinct values.
给定 pushed
和 popped
两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true
;否则,返回 false
。
示例 1:
输入:pushed = [1,2,3,4,5], popped = [4,5,3,2,1] 输出:true 解释:我们可以按以下顺序执行: push(1), push(2), push(3), push(4), pop() -> 4, push(5), pop() -> 5, pop() -> 3, pop() -> 2, pop() -> 1
示例 2:
输入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2] 输出:false 解释:1 不能在 2 之前弹出。
提示:
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
是popped
的排列。
64ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 var i = 0; 4 5 var result = [Int]() 6 for element in pushed { 7 result.append(element) 8 9 while !result.isEmpty && result.last! == popped[i] { 10 result.removeLast() 11 i += 1 12 } 13 } 14 15 return result.count == 0 ? true : false 16 } 17 }
68ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 var pop = popped 4 var stack: [Int] = [] 5 var pushCount = pushed.count 6 var i = 0 7 while !pop.isEmpty && i < pushCount { 8 stack.append(pushed[i]) 9 i += 1 10 while let first = pop.first, let last = stack.last, first == last { 11 pop.remove(at: 0) 12 stack.remove(at: stack.count - 1) 13 } 14 } 15 return pop.count == 0 16 17 } 18 }
72ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 var s:Stack<Int> = Stack<Int>() 4 var j:Int = 0 5 for i in 0..<pushed.count 6 { 7 s.push(pushed[i]) 8 while (!s.isEmpty() && j < popped.count && s.getLast() == popped[j]) 9 { 10 s.pop() 11 j += 1 12 } 13 } 14 return s.isEmpty() && j == popped.count 15 } 16 } 17 18 public struct Stack<T> { 19 20 // 泛型数组:用于存储数据元素 21 fileprivate var stack: [T] 22 23 // 返回堆栈中元素的个数 24 public var count: Int { 25 return stack.count 26 } 27 28 /// 构造函数:创建一个空的堆栈 29 public init() { 30 stack = [T]() 31 } 32 33 // 检查堆栈是否为空 34 // - returns: 如果堆栈为空,则返回true,否则返回false 35 public func isEmpty() -> Bool { 36 return stack.isEmpty 37 } 38 39 // 入堆栈操作:将元素添加到堆栈的末尾 40 public mutating func push(_ element: T) { 41 stack.append(element) 42 } 43 44 // 出堆栈操作:删除并返回堆栈中的第一个元素 45 public mutating func pop() -> T? { 46 return stack.removeLast() 47 } 48 49 // 返回堆栈中的第一个元素(不删除) 50 public func getLast() -> T? { 51 return stack.last! 52 } 53 }
72ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 var push = pushed 4 var pop = popped 5 var qs = [Int]() 6 while let u = pop.first { 7 if let v = qs.last, v == u { 8 qs.removeLast() 9 pop.remove(at: 0) 10 continue 11 } 12 if let v = push.first, v == u { 13 push.remove(at: 0) 14 pop.remove(at: 0) 15 continue 16 } 17 guard !push.isEmpty else { 18 return false 19 } 20 let r = push.remove(at: 0) 21 qs.append(r) 22 } 23 guard push.isEmpty, pop.isEmpty else { 24 return false 25 } 26 return true 27 } 28 }
80ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 var popped = popped 4 var stack = [Int]() 5 for item in pushed { 6 stack.append(item) 7 while stack.last == popped.first && popped.first != nil { 8 stack.removeLast() 9 popped.removeFirst() 10 } 11 } 12 while stack.last == popped.first && popped.first != nil { 13 stack.removeLast() 14 popped.removeFirst() 15 } 16 return stack.isEmpty 17 } 18 }
92ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 var currentStack: [Int] = [] 4 var nextPushIndex = 0 5 var nextPopIndex = 0 6 7 while nextPopIndex < popped.count { 8 let poppedValue = popped[nextPopIndex] 9 10 while currentStack.last != poppedValue && nextPushIndex < pushed.count { 11 currentStack.append(pushed[nextPushIndex]) 12 nextPushIndex += 1 13 } 14 15 if currentStack[currentStack.count - 1] != poppedValue { 16 return false 17 } else { 18 currentStack.removeLast() 19 } 20 21 nextPopIndex += 1 22 } 23 24 return true 25 } 26 }
100ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 guard pushed.count == popped.count else { return false } 4 var st: [Int] = [] 5 6 var pushIt = 0 7 var popIt = 0 8 9 while pushIt < pushed.count { 10 st.append(pushed[pushIt]) 11 //print(st, pushIt, popIt) 12 while st.count > 0 && st[st.count - 1] == popped[popIt] { 13 //print("del ", st, pushIt, popIt) 14 st.remove(at: st.count - 1) 15 popIt += 1 16 } 17 pushIt += 1 18 } 19 20 return st.count == 0 21 } 22 }
104ms
1 class Solution { 2 func validateStackSequences(_ pushed: [Int], _ popped: [Int]) -> Bool { 3 if (pushed.count != popped.count) { 4 return false 5 } 6 7 var stack = [Int]() 8 var N = pushed.count 9 var j = 0 10 11 12 for x in pushed { 13 stack.append(x) 14 while(stack.count > 0 && 15 j < N && 16 stack.last == popped[j]) { 17 stack.popLast() 18 j += 1 19 } 20 } 21 22 return j == N 23 } 24 }