Skip to content

feat: add swift implementation to lcci problem: No.03.01 #2631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions lcci/03.01.Three in One/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,53 @@ class TripleInOne {
*/
```

```swift
class TripleInOne {
private var cap: Int
private var stk: [Int]

init(_ stackSize: Int) {
self.cap = stackSize
self.stk = [Int](repeating: 0, count: cap * 3 + 3)
}

func push(_ stackNum: Int, _ value: Int) {
if stk[cap * 3 + stackNum] < cap {
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value
stk[cap * 3 + stackNum] += 1
}
}

func pop(_ stackNum: Int) -> Int {
if isEmpty(stackNum) {
return -1
}
stk[cap * 3 + stackNum] -= 1
return stk[cap * stackNum + stk[cap * 3 + stackNum]]
}

func peek(_ stackNum: Int) -> Int {
if isEmpty(stackNum) {
return -1
}
return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]
}

func isEmpty(_ stackNum: Int) -> Bool {
return stk[cap * 3 + stackNum] == 0
}
}

/**
* Your TripleInOne object will be instantiated and called as such:
* let obj = TripleInOne(stackSize)
* obj.push(stackNum,value)
* let param_2 = obj.pop(stackNum)
* let param_3 = obj.peek(stackNum)
* let param_4 = obj.isEmpty(stackNum)
*/
```

<!-- tabs:end -->

<!-- end -->
47 changes: 47 additions & 0 deletions lcci/03.01.Three in One/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,53 @@ class TripleInOne {
*/
```

```swift
class TripleInOne {
private var cap: Int
private var stk: [Int]

init(_ stackSize: Int) {
self.cap = stackSize
self.stk = [Int](repeating: 0, count: cap * 3 + 3)
}

func push(_ stackNum: Int, _ value: Int) {
if stk[cap * 3 + stackNum] < cap {
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value
stk[cap * 3 + stackNum] += 1
}
}

func pop(_ stackNum: Int) -> Int {
if isEmpty(stackNum) {
return -1
}
stk[cap * 3 + stackNum] -= 1
return stk[cap * stackNum + stk[cap * 3 + stackNum]]
}

func peek(_ stackNum: Int) -> Int {
if isEmpty(stackNum) {
return -1
}
return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]
}

func isEmpty(_ stackNum: Int) -> Bool {
return stk[cap * 3 + stackNum] == 0
}
}

/**
* Your TripleInOne object will be instantiated and called as such:
* let obj = TripleInOne(stackSize)
* obj.push(stackNum,value)
* let param_2 = obj.pop(stackNum)
* let param_3 = obj.peek(stackNum)
* let param_4 = obj.isEmpty(stackNum)
*/
```

<!-- tabs:end -->

<!-- end -->
44 changes: 44 additions & 0 deletions lcci/03.01.Three in One/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class TripleInOne {
private var cap: Int
private var stk: [Int]

init(_ stackSize: Int) {
self.cap = stackSize
self.stk = [Int](repeating: 0, count: cap * 3 + 3)
}

func push(_ stackNum: Int, _ value: Int) {
if stk[cap * 3 + stackNum] < cap {
stk[cap * stackNum + stk[cap * 3 + stackNum]] = value
stk[cap * 3 + stackNum] += 1
}
}

func pop(_ stackNum: Int) -> Int {
if isEmpty(stackNum) {
return -1
}
stk[cap * 3 + stackNum] -= 1
return stk[cap * stackNum + stk[cap * 3 + stackNum]]
}

func peek(_ stackNum: Int) -> Int {
if isEmpty(stackNum) {
return -1
}
return stk[cap * stackNum + stk[cap * 3 + stackNum] - 1]
}

func isEmpty(_ stackNum: Int) -> Bool {
return stk[cap * 3 + stackNum] == 0
}
}

/**
* Your TripleInOne object will be instantiated and called as such:
* let obj = TripleInOne(stackSize)
* obj.push(stackNum,value)
* let param_2 = obj.pop(stackNum)
* let param_3 = obj.peek(stackNum)
* let param_4 = obj.isEmpty(stackNum)
*/