Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4b1700a

Browse files
feat: add swift implementation to lcci problem: No.03.04 (doocs#2642)
1 parent 2e7ed8a commit 4b1700a

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

‎lcci/03.04.Implement Queue using Stacks/README.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,52 @@ impl MyQueue {
299299
*/
300300
```
301301

302+
```swift
303+
class MyQueue {
304+
private var stk1: [Int] = []
305+
private var stk2: [Int] = []
306+
307+
init() {}
308+
309+
func push(_ x: Int) {
310+
stk1.append(x)
311+
}
312+
313+
@discardableResult
314+
func pop() -> Int {
315+
move()
316+
return stk2.removeLast()
317+
}
318+
319+
func peek() -> Int {
320+
move()
321+
return stk2.last!
322+
}
323+
324+
func empty() -> Bool {
325+
return stk1.isEmpty && stk2.isEmpty
326+
}
327+
328+
private func move() {
329+
if stk2.isEmpty {
330+
while !stk1.isEmpty {
331+
stk2.append(stk1.removeLast())
332+
}
333+
}
334+
}
335+
}
336+
337+
/**
338+
* Your MyQueue object will be instantiated and called as such:
339+
* let obj = new MyQueue();
340+
* obj.push(x);
341+
* let param_2 = obj.pop();
342+
* let param_3 = obj.peek();
343+
* var myValue : Bool
344+
* myValue = obj.empty();
345+
*/
346+
```
347+
302348
<!-- tabs:end -->
303349

304350
<!-- end -->

‎lcci/03.04.Implement Queue using Stacks/README_EN.md‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,52 @@ impl MyQueue {
330330
*/
331331
```
332332

333+
```swift
334+
class MyQueue {
335+
private var stk1: [Int] = []
336+
private var stk2: [Int] = []
337+
338+
init() {}
339+
340+
func push(_ x: Int) {
341+
stk1.append(x)
342+
}
343+
344+
@discardableResult
345+
func pop() -> Int {
346+
move()
347+
return stk2.removeLast()
348+
}
349+
350+
func peek() -> Int {
351+
move()
352+
return stk2.last!
353+
}
354+
355+
func empty() -> Bool {
356+
return stk1.isEmpty && stk2.isEmpty
357+
}
358+
359+
private func move() {
360+
if stk2.isEmpty {
361+
while !stk1.isEmpty {
362+
stk2.append(stk1.removeLast())
363+
}
364+
}
365+
}
366+
}
367+
368+
/**
369+
* Your MyQueue object will be instantiated and called as such:
370+
* let obj = new MyQueue();
371+
* obj.push(x);
372+
* let param_2 = obj.pop();
373+
* let param_3 = obj.peek();
374+
* var myValue : Bool
375+
* myValue = obj.empty();
376+
*/
377+
```
378+
333379
<!-- tabs:end -->
334380

335381
<!-- end -->
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class MyQueue {
2+
private var stk1: [Int] = []
3+
private var stk2: [Int] = []
4+
5+
init() {}
6+
7+
func push(_ x: Int) {
8+
stk1.append(x)
9+
}
10+
11+
@discardableResult
12+
func pop() -> Int {
13+
move()
14+
return stk2.removeLast()
15+
}
16+
17+
func peek() -> Int {
18+
move()
19+
return stk2.last!
20+
}
21+
22+
func empty() -> Bool {
23+
return stk1.isEmpty && stk2.isEmpty
24+
}
25+
26+
private func move() {
27+
if stk2.isEmpty {
28+
while !stk1.isEmpty {
29+
stk2.append(stk1.removeLast())
30+
}
31+
}
32+
}
33+
}
34+
35+
/**
36+
* Your MyQueue object will be instantiated and called as such:
37+
* let obj = new MyQueue();
38+
* obj.push(x);
39+
* let param_2 = obj.pop();
40+
* let param_3 = obj.peek();
41+
* var myValue : Bool
42+
* myValue = obj.empty();
43+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /