File tree 2 files changed +37
-0
lines changed
2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -208,6 +208,27 @@ public class Solution {
208
208
}
209
209
```
210
210
211
+ #### Swift
212
+
213
+ ``` swift
214
+ class Solution {
215
+ func validateStackSequences (_ pushed : [Int ], _ popped : [Int ]) -> Bool {
216
+ var stack = [Int ]()
217
+ var j = 0
218
+
219
+ for v in pushed {
220
+ stack.append (v)
221
+ while ! stack.isEmpty && stack.last == popped[j] {
222
+ stack.removeLast ()
223
+ j += 1
224
+ }
225
+ }
226
+
227
+ return j == pushed.count
228
+ }
229
+ }
230
+ ```
231
+
211
232
<!-- tabs: end -->
212
233
213
234
<!-- solution: end -->
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func validateStackSequences( _ pushed: [ Int ] , _ popped: [ Int ] ) -> Bool {
3
+ var stack = [ Int] ( )
4
+ var j = 0
5
+
6
+ for v in pushed {
7
+ stack. append ( v)
8
+ while !stack. isEmpty && stack. last == popped [ j] {
9
+ stack. removeLast ( )
10
+ j += 1
11
+ }
12
+ }
13
+
14
+ return j == pushed. count
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments