Skip to content

Commit 7afdf4e

Browse files
authored
feat: add swift implementation to lcci problem: No.03.01 (#2631)
1 parent b05aa50 commit 7afdf4e

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

lcci/03.01.Three in One/README.md

+47
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,53 @@ class TripleInOne {
266266
*/
267267
```
268268

269+
```swift
270+
class TripleInOne {
271+
private var cap: Int
272+
private var stk: [Int]
273+
274+
init(_ stackSize: Int) {
275+
self.cap = stackSize
276+
self.stk = [Int](repeating: 0, count: cap * 3 + 3)
277+
}
278+
279+
func push(_ stackNum: Int, _ value: Int) {
280+
if stk[cap * 3 + stackNum] < cap {
281+
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value
282+
stk[cap * 3 + stackNum] += 1
283+
}
284+
}
285+
286+
func pop(_ stackNum: Int) -> Int {
287+
if isEmpty(stackNum) {
288+
return -1
289+
}
290+
stk[cap * 3 + stackNum] -= 1
291+
return stk[cap * stackNum + stk[cap * 3 + stackNum]]
292+
}
293+
294+
func peek(_ stackNum: Int) -> Int {
295+
if isEmpty(stackNum) {
296+
return -1
297+
}
298+
return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]
299+
}
300+
301+
func isEmpty(_ stackNum: Int) -> Bool {
302+
return stk[cap * 3 + stackNum] == 0
303+
}
304+
}
305+
306+
/**
307+
* Your TripleInOne object will be instantiated and called as such:
308+
* let obj = TripleInOne(stackSize)
309+
* obj.push(stackNum,value)
310+
* let param_2 = obj.pop(stackNum)
311+
* let param_3 = obj.peek(stackNum)
312+
* let param_4 = obj.isEmpty(stackNum)
313+
*/
314+
```
315+
269316
<!-- tabs:end -->
270317

271318
<!-- end -->

lcci/03.01.Three in One/README_EN.md

+47
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,53 @@ class TripleInOne {
280280
*/
281281
```
282282

283+
```swift
284+
class TripleInOne {
285+
private var cap: Int
286+
private var stk: [Int]
287+
288+
init(_ stackSize: Int) {
289+
self.cap = stackSize
290+
self.stk = [Int](repeating: 0, count: cap * 3 + 3)
291+
}
292+
293+
func push(_ stackNum: Int, _ value: Int) {
294+
if stk[cap * 3 + stackNum] < cap {
295+
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value
296+
stk[cap * 3 + stackNum] += 1
297+
}
298+
}
299+
300+
func pop(_ stackNum: Int) -> Int {
301+
if isEmpty(stackNum) {
302+
return -1
303+
}
304+
stk[cap * 3 + stackNum] -= 1
305+
return stk[cap * stackNum + stk[cap * 3 + stackNum]]
306+
}
307+
308+
func peek(_ stackNum: Int) -> Int {
309+
if isEmpty(stackNum) {
310+
return -1
311+
}
312+
return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]
313+
}
314+
315+
func isEmpty(_ stackNum: Int) -> Bool {
316+
return stk[cap * 3 + stackNum] == 0
317+
}
318+
}
319+
320+
/**
321+
* Your TripleInOne object will be instantiated and called as such:
322+
* let obj = TripleInOne(stackSize)
323+
* obj.push(stackNum,value)
324+
* let param_2 = obj.pop(stackNum)
325+
* let param_3 = obj.peek(stackNum)
326+
* let param_4 = obj.isEmpty(stackNum)
327+
*/
328+
```
329+
283330
<!-- tabs:end -->
284331

285332
<!-- end -->
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class TripleInOne {
2+
private var cap: Int
3+
private var stk: [Int]
4+
5+
init(_ stackSize: Int) {
6+
self.cap = stackSize
7+
self.stk = [Int](repeating: 0, count: cap * 3 + 3)
8+
}
9+
10+
func push(_ stackNum: Int, _ value: Int) {
11+
if stk[cap * 3 + stackNum] < cap {
12+
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value
13+
stk[cap * 3 + stackNum] += 1
14+
}
15+
}
16+
17+
func pop(_ stackNum: Int) -> Int {
18+
if isEmpty(stackNum) {
19+
return -1
20+
}
21+
stk[cap * 3 + stackNum] -= 1
22+
return stk[cap * stackNum + stk[cap * 3 + stackNum]]
23+
}
24+
25+
func peek(_ stackNum: Int) -> Int {
26+
if isEmpty(stackNum) {
27+
return -1
28+
}
29+
return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]
30+
}
31+
32+
func isEmpty(_ stackNum: Int) -> Bool {
33+
return stk[cap * 3 + stackNum] == 0
34+
}
35+
}
36+
37+
/**
38+
* Your TripleInOne object will be instantiated and called as such:
39+
* let obj = TripleInOne(stackSize)
40+
* obj.push(stackNum,value)
41+
* let param_2 = obj.pop(stackNum)
42+
* let param_3 = obj.peek(stackNum)
43+
* let param_4 = obj.isEmpty(stackNum)
44+
*/

0 commit comments

Comments
 (0)