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 lcof problem: No.30 #2897

Merged
merged 1 commit into from
May 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
41 changes: 41 additions & 0 deletions lcof/面试题30. 包含min函数的栈/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,47 @@ public class MinStack {
*/
```

#### Swift

```swift
class MinStack {
private var stack: [Int]
private var minStack: [Int]

init() {
stack = []
minStack = [Int.max]
}

func push(_ x: Int) {
stack.append(x)
minStack.append(min(x, minStack.last!))
}

func pop() {
stack.removeLast()
minStack.removeLast()
}

func top() -> Int {
return stack.last!
}

func getMin() -> Int {
return minStack.last!
}
}

/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack();
* obj.push(x);
* obj.pop();
* let param_3 = obj.top();
* let param_4 = obj.getMin();
*/
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
36 changes: 36 additions & 0 deletions lcof/面试题30. 包含min函数的栈/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class MinStack {
private var stack: [Int]
private var minStack: [Int]

init() {
stack = []
minStack = [Int.max]
}

func push(_ x: Int) {
stack.append(x)
minStack.append(min(x, minStack.last!))
}

func pop() {
stack.removeLast()
minStack.removeLast()
}

func top() -> Int {
return stack.last!
}

func getMin() -> Int {
return minStack.last!
}
}

/**
* Your MinStack object will be instantiated and called as such:
* let obj = MinStack();
* obj.push(x);
* obj.pop();
* let param_3 = obj.top();
* let param_4 = obj.getMin();
*/
Loading