Skip to content
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

feat: add swift implementation to lcci problem: No.03.04 #2642

Merged
merged 3 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
46 changes: 46 additions & 0 deletions lcci/03.04.Implement Queue using Stacks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,52 @@ impl MyQueue {
*/
```

```swift
class MyQueue {
private var stk1: [Int] = []
private var stk2: [Int] = []

init() {}

func push(_ x: Int) {
stk1.append(x)
}

@discardableResult
func pop() -> Int {
move()
return stk2.removeLast()
}

func peek() -> Int {
move()
return stk2.last!
}

func empty() -> Bool {
return stk1.isEmpty && stk2.isEmpty
}

private func move() {
if stk2.isEmpty {
while !stk1.isEmpty {
stk2.append(stk1.removeLast())
}
}
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* let obj = new MyQueue();
* obj.push(x);
* let param_2 = obj.pop();
* let param_3 = obj.peek();
* var myValue : Bool
* myValue = obj.empty();
*/
```

<!-- tabs:end -->

<!-- end -->
46 changes: 46 additions & 0 deletions lcci/03.04.Implement Queue using Stacks/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,52 @@ impl MyQueue {
*/
```

```swift
class MyQueue {
private var stk1: [Int] = []
private var stk2: [Int] = []

init() {}

func push(_ x: Int) {
stk1.append(x)
}

@discardableResult
func pop() -> Int {
move()
return stk2.removeLast()
}

func peek() -> Int {
move()
return stk2.last!
}

func empty() -> Bool {
return stk1.isEmpty && stk2.isEmpty
}

private func move() {
if stk2.isEmpty {
while !stk1.isEmpty {
stk2.append(stk1.removeLast())
}
}
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* let obj = new MyQueue();
* obj.push(x);
* let param_2 = obj.pop();
* let param_3 = obj.peek();
* var myValue : Bool
* myValue = obj.empty();
*/
```

<!-- tabs:end -->

<!-- end -->
43 changes: 43 additions & 0 deletions lcci/03.04.Implement Queue using Stacks/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class MyQueue {
private var stk1: [Int] = []
private var stk2: [Int] = []

init() {}

func push(_ x: Int) {
stk1.append(x)
}

@discardableResult
func pop() -> Int {
move()
return stk2.removeLast()
}

func peek() -> Int {
move()
return stk2.last!
}

func empty() -> Bool {
return stk1.isEmpty && stk2.isEmpty
}

private func move() {
if stk2.isEmpty {
while !stk1.isEmpty {
stk2.append(stk1.removeLast())
}
}
}
}

/**
* Your MyQueue object will be instantiated and called as such:
* let obj = new MyQueue();
* obj.push(x);
* let param_2 = obj.pop();
* let param_3 = obj.peek();
* var myValue : Bool
* myValue = obj.empty();
*/
Loading