Skip to content

Commit 2e7ed8a

Browse files
authored
feat: add swift implementation to lcci problem: No.03.03 (doocs#2641)
1 parent bd06665 commit 2e7ed8a

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed

lcci/03.03.Stack of Plates/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,51 @@ class StackOfPlates {
258258
*/
259259
```
260260

261+
```swift
262+
class StackOfPlates {
263+
private var stacks: [[Int]]
264+
private var cap: Int
265+
266+
init(_ cap: Int) {
267+
self.cap = cap
268+
self.stacks = []
269+
}
270+
271+
func push(_ val: Int) {
272+
if cap == 0 {
273+
return
274+
}
275+
if stacks.isEmpty || stacks.last!.count >= cap {
276+
stacks.append([])
277+
}
278+
stacks[stacks.count - 1].append(val)
279+
}
280+
281+
func pop() -> Int {
282+
return popAt(stacks.count - 1)
283+
}
284+
285+
func popAt(_ index: Int) -> Int {
286+
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
287+
return -1
288+
}
289+
let value = stacks[index].removeLast()
290+
if stacks[index].isEmpty {
291+
stacks.remove(at: index)
292+
}
293+
return value
294+
}
295+
}
296+
297+
/**
298+
* Your StackOfPlates object will be instantiated and called as such:
299+
* let obj = new StackOfPlates(cap);
300+
* obj.push(val);
301+
* let param_2 = obj.pop();
302+
* let param_3 = obj.popAt(index);
303+
*/
304+
```
305+
261306
<!-- tabs:end -->
262307

263308
<!-- end -->

lcci/03.03.Stack of Plates/README_EN.md

+45
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,51 @@ class StackOfPlates {
273273
*/
274274
```
275275

276+
```swift
277+
class StackOfPlates {
278+
private var stacks: [[Int]]
279+
private var cap: Int
280+
281+
init(_ cap: Int) {
282+
self.cap = cap
283+
self.stacks = []
284+
}
285+
286+
func push(_ val: Int) {
287+
if cap == 0 {
288+
return
289+
}
290+
if stacks.isEmpty || stacks.last!.count >= cap {
291+
stacks.append([])
292+
}
293+
stacks[stacks.count - 1].append(val)
294+
}
295+
296+
func pop() -> Int {
297+
return popAt(stacks.count - 1)
298+
}
299+
300+
func popAt(_ index: Int) -> Int {
301+
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
302+
return -1
303+
}
304+
let value = stacks[index].removeLast()
305+
if stacks[index].isEmpty {
306+
stacks.remove(at: index)
307+
}
308+
return value
309+
}
310+
}
311+
312+
/**
313+
* Your StackOfPlates object will be instantiated and called as such:
314+
* let obj = new StackOfPlates(cap);
315+
* obj.push(val);
316+
* let param_2 = obj.pop();
317+
* let param_3 = obj.popAt(index);
318+
*/
319+
```
320+
276321
<!-- tabs:end -->
277322

278323
<!-- end -->
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class StackOfPlates {
2+
private var stacks: [[Int]]
3+
private var cap: Int
4+
5+
init(_ cap: Int) {
6+
self.cap = cap
7+
self.stacks = []
8+
}
9+
10+
func push(_ val: Int) {
11+
if cap == 0 {
12+
return
13+
}
14+
if stacks.isEmpty || stacks.last!.count >= cap {
15+
stacks.append([])
16+
}
17+
stacks[stacks.count - 1].append(val)
18+
}
19+
20+
func pop() -> Int {
21+
return popAt(stacks.count - 1)
22+
}
23+
24+
func popAt(_ index: Int) -> Int {
25+
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
26+
return -1
27+
}
28+
let value = stacks[index].removeLast()
29+
if stacks[index].isEmpty {
30+
stacks.remove(at: index)
31+
}
32+
return value
33+
}
34+
}
35+
36+
/**
37+
* Your StackOfPlates object will be instantiated and called as such:
38+
* let obj = new StackOfPlates(cap);
39+
* obj.push(val);
40+
* let param_2 = obj.pop();
41+
* let param_3 = obj.popAt(index);
42+
*/

0 commit comments

Comments
 (0)