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 lcp problem: No.44 #3784

Merged
merged 1 commit into from
Nov 19, 2024
Merged
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
32 changes: 32 additions & 0 deletions lcp/LCP 44. 开幕式焰火/README.md
Original file line number Diff line number Diff line change
@@ -159,6 +159,38 @@ func dfs(root *TreeNode) {
}
```

#### Swift

```swift
/* class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
* init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/

class Solution {
private var uniqueColors: Set<Int> = []

func numColor(_ root: TreeNode?) -> Int {
dfs(root)
return uniqueColors.count
}

private func dfs(_ node: TreeNode?) {
guard let node = node else { return }
uniqueColors.insert(node.val)
dfs(node.left)
dfs(node.right)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
27 changes: 27 additions & 0 deletions lcp/LCP 44. 开幕式焰火/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* class TreeNode {
* var val: Int
* var left: TreeNode?
* var right: TreeNode?
* init(_ val: Int) {
* self.val = val
* self.left = nil
* self.right = nil
* }
* }
*/

class Solution {
private var uniqueColors: Set<Int> = []

func numColor(_ root: TreeNode?) -> Int {
dfs(root)
return uniqueColors.count
}

private func dfs(_ node: TreeNode?) {
guard let node = node else { return }
uniqueColors.insert(node.val)
dfs(node.left)
dfs(node.right)
}
}