Skip to content

Commit aee9359

Browse files
authored
feat: add swift implementation to lcci problem: No.03.06 (doocs#2644)
1 parent 33b4b5a commit aee9359

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

lcci/03.06.Animal Shelter/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,43 @@ impl AnimalShelf {
316316
*/
317317
```
318318

319+
```swift
320+
class AnimalShelf {
321+
private var q: [[Int]] = Array(repeating: [], count: 2)
322+
323+
init() {
324+
}
325+
326+
func enqueue(_ animal: [Int]) {
327+
q[animal[1]].append(animal[0])
328+
}
329+
330+
func dequeueAny() -> [Int] {
331+
if q[0].isEmpty || (!q[1].isEmpty && q[1].first! < q[0].first!) {
332+
return dequeueDog()
333+
}
334+
return dequeueCat()
335+
}
336+
337+
func dequeueDog() -> [Int] {
338+
return q[1].isEmpty ? [-1, -1] : [q[1].removeFirst(), 1]
339+
}
340+
341+
func dequeueCat() -> [Int] {
342+
return q[0].isEmpty ? [-1, -1] : [q[0].removeFirst(), 0]
343+
}
344+
}
345+
346+
/**
347+
* Your AnimalShelf object will be instantiated and called as such:
348+
* let obj = new AnimalShelf();
349+
* obj.enqueue(animal);
350+
* let param_2 = obj.dequeueAny();
351+
* let param_3 = obj.dequeueDog();
352+
* let param_4 = obj.dequeueCat();
353+
*/
354+
```
355+
319356
<!-- tabs:end -->
320357

321358
<!-- end -->

lcci/03.06.Animal Shelter/README_EN.md

+37
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,43 @@ impl AnimalShelf {
329329
*/
330330
```
331331

332+
```swift
333+
class AnimalShelf {
334+
private var q: [[Int]] = Array(repeating: [], count: 2)
335+
336+
init() {
337+
}
338+
339+
func enqueue(_ animal: [Int]) {
340+
q[animal[1]].append(animal[0])
341+
}
342+
343+
func dequeueAny() -> [Int] {
344+
if q[0].isEmpty || (!q[1].isEmpty && q[1].first! < q[0].first!) {
345+
return dequeueDog()
346+
}
347+
return dequeueCat()
348+
}
349+
350+
func dequeueDog() -> [Int] {
351+
return q[1].isEmpty ? [-1, -1] : [q[1].removeFirst(), 1]
352+
}
353+
354+
func dequeueCat() -> [Int] {
355+
return q[0].isEmpty ? [-1, -1] : [q[0].removeFirst(), 0]
356+
}
357+
}
358+
359+
/**
360+
* Your AnimalShelf object will be instantiated and called as such:
361+
* let obj = new AnimalShelf();
362+
* obj.enqueue(animal);
363+
* let param_2 = obj.dequeueAny();
364+
* let param_3 = obj.dequeueDog();
365+
* let param_4 = obj.dequeueCat();
366+
*/
367+
```
368+
332369
<!-- tabs:end -->
333370

334371
<!-- end -->
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class AnimalShelf {
2+
private var q: [[Int]] = Array(repeating: [], count: 2)
3+
4+
init() {
5+
}
6+
7+
func enqueue(_ animal: [Int]) {
8+
q[animal[1]].append(animal[0])
9+
}
10+
11+
func dequeueAny() -> [Int] {
12+
if q[0].isEmpty || (!q[1].isEmpty && q[1].first! < q[0].first!) {
13+
return dequeueDog()
14+
}
15+
return dequeueCat()
16+
}
17+
18+
func dequeueDog() -> [Int] {
19+
return q[1].isEmpty ? [-1, -1] : [q[1].removeFirst(), 1]
20+
}
21+
22+
func dequeueCat() -> [Int] {
23+
return q[0].isEmpty ? [-1, -1] : [q[0].removeFirst(), 0]
24+
}
25+
}
26+
27+
/**
28+
* Your AnimalShelf object will be instantiated and called as such:
29+
* let obj = new AnimalShelf();
30+
* obj.enqueue(animal);
31+
* let param_2 = obj.dequeueAny();
32+
* let param_3 = obj.dequeueDog();
33+
* let param_4 = obj.dequeueCat();
34+
*/
35+

0 commit comments

Comments
 (0)