Skip to content

feat: add swift implementation to lcci problem: No.03.03 #2641

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 2 commits into from
Apr 23, 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
45 changes: 45 additions & 0 deletions lcci/03.03.Stack of Plates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,51 @@ class StackOfPlates {
*/
```

```swift
class StackOfPlates {
private var stacks: [[Int]]
private var cap: Int

init(_ cap: Int) {
self.cap = cap
self.stacks = []
}

func push(_ val: Int) {
if cap == 0 {
return
}
if stacks.isEmpty || stacks.last!.count >= cap {
stacks.append([])
}
stacks[stacks.count - 1].append(val)
}

func pop() -> Int {
return popAt(stacks.count - 1)
}

func popAt(_ index: Int) -> Int {
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
return -1
}
let value = stacks[index].removeLast()
if stacks[index].isEmpty {
stacks.remove(at: index)
}
return value
}
}

/**
* Your StackOfPlates object will be instantiated and called as such:
* let obj = new StackOfPlates(cap);
* obj.push(val);
* let param_2 = obj.pop();
* let param_3 = obj.popAt(index);
*/
```

<!-- tabs:end -->

<!-- end -->
45 changes: 45 additions & 0 deletions lcci/03.03.Stack of Plates/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,51 @@ class StackOfPlates {
*/
```

```swift
class StackOfPlates {
private var stacks: [[Int]]
private var cap: Int

init(_ cap: Int) {
self.cap = cap
self.stacks = []
}

func push(_ val: Int) {
if cap == 0 {
return
}
if stacks.isEmpty || stacks.last!.count >= cap {
stacks.append([])
}
stacks[stacks.count - 1].append(val)
}

func pop() -> Int {
return popAt(stacks.count - 1)
}

func popAt(_ index: Int) -> Int {
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
return -1
}
let value = stacks[index].removeLast()
if stacks[index].isEmpty {
stacks.remove(at: index)
}
return value
}
}

/**
* Your StackOfPlates object will be instantiated and called as such:
* let obj = new StackOfPlates(cap);
* obj.push(val);
* let param_2 = obj.pop();
* let param_3 = obj.popAt(index);
*/
```

<!-- tabs:end -->

<!-- end -->
42 changes: 42 additions & 0 deletions lcci/03.03.Stack of Plates/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class StackOfPlates {
private var stacks: [[Int]]
private var cap: Int

init(_ cap: Int) {
self.cap = cap
self.stacks = []
}

func push(_ val: Int) {
if cap == 0 {
return
}
if stacks.isEmpty || stacks.last!.count >= cap {
stacks.append([])
}
stacks[stacks.count - 1].append(val)
}

func pop() -> Int {
return popAt(stacks.count - 1)
}

func popAt(_ index: Int) -> Int {
guard index >= 0, index < stacks.count, !stacks[index].isEmpty else {
return -1
}
let value = stacks[index].removeLast()
if stacks[index].isEmpty {
stacks.remove(at: index)
}
return value
}
}

/**
* Your StackOfPlates object will be instantiated and called as such:
* let obj = new StackOfPlates(cap);
* obj.push(val);
* let param_2 = obj.pop();
* let param_3 = obj.popAt(index);
*/