Skip to content

Commit 5cc6315

Browse files
authored
feat: add swift implementation to lcof problem: No.09 (#2855)
1 parent d2ee6aa commit 5cc6315

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lcof/面试题09. 用两个栈实现队列/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,38 @@ public class CQueue {
336336
*/
337337
```
338338

339+
#### Swift
340+
341+
```swift
342+
class CQueue {
343+
private var stk1: [Int] = []
344+
private var stk2: [Int] = []
345+
346+
init() {
347+
}
348+
349+
func appendTail(_ value: Int) {
350+
stk1.append(value)
351+
}
352+
353+
func deleteHead() -> Int {
354+
if stk2.isEmpty {
355+
while !stk1.isEmpty {
356+
stk2.append(stk1.removeLast())
357+
}
358+
}
359+
return stk2.isEmpty ? -1 : stk2.removeLast()
360+
}
361+
}
362+
363+
/**
364+
* Your CQueue object will be instantiated and called as such:
365+
* let obj = CQueue();
366+
* obj.appendTail(value);
367+
* let param_2 = obj.DeleteHead();
368+
*/
369+
```
370+
339371
<!-- tabs:end -->
340372

341373
<!-- solution:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class CQueue {
2+
private var stk1: [Int] = []
3+
private var stk2: [Int] = []
4+
5+
init() {
6+
}
7+
8+
func appendTail(_ value: Int) {
9+
stk1.append(value)
10+
}
11+
12+
func deleteHead() -> Int {
13+
if stk2.isEmpty {
14+
while !stk1.isEmpty {
15+
stk2.append(stk1.removeLast())
16+
}
17+
}
18+
return stk2.isEmpty ? -1 : stk2.removeLast()
19+
}
20+
}

0 commit comments

Comments
 (0)