★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-osfnhdmy-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★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.git
Example 1:github
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 of popped
.pushed
and popped
have distinct values.給定 pushed
和 popped
兩個序列,只有當它們多是在最初空棧上進行的推入 push 和彈出 pop 操做序列的結果時,返回 true
;不然,返回 false
。app
示例 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:this
輸入:pushed = [1,2,3,4,5], popped = [4,3,5,1,2] 輸出:false 解釋:1 不能在 2 以前彈出。
提示:spa
0 <= pushed.length == popped.length <= 1000
0 <= pushed[i], popped[i] < 1000
pushed
是 popped
的排列。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 }
68mscode
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 }
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 }
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 }