Skip to content

feat: add swift implementation to lcp problem: No.52 #3787

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 1 commit into from
Nov 19, 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
57 changes: 57 additions & 0 deletions lcp/LCP 52. 二叉搜索树染色/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,63 @@ class TreeSet<T = number> {
}
```

#### Swift

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

class Solution {
private var treeValues: [Int] = []

func getNumber(_ root: TreeNode?, _ ops: [[Int]]) -> Int {
collectValues(root)

treeValues.sort()

var ans = 0
for op in ops.reversed() {
let t = op[0]
let x = op[1]
let y = op[2]
var indicesToRemove: [Int] = []

for i in 0..<treeValues.count {
let val = treeValues[i]
if val >= x && val <= y {
indicesToRemove.append(i)
ans += t
}
}

for index in indicesToRemove.reversed() {
treeValues.remove(at: index)
}
}

return ans
}

private func collectValues(_ root: TreeNode?) {
guard let root = root else { return }
treeValues.append(root.val)
collectValues(root.left)
collectValues(root.right)
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
52 changes: 52 additions & 0 deletions lcp/LCP 52. 二叉搜索树染色/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/

class Solution {
private var treeValues: [Int] = []

func getNumber(_ root: TreeNode?, _ ops: [[Int]]) -> Int {
collectValues(root)

treeValues.sort()

var ans = 0
for op in ops.reversed() {
let t = op[0]
let x = op[1]
let y = op[2]
var indicesToRemove: [Int] = []

for i in 0..<treeValues.count {
let val = treeValues[i]
if val >= x && val <= y {
indicesToRemove.append(i)
ans += t
}
}

for index in indicesToRemove.reversed() {
treeValues.remove(at: index)
}
}

return ans
}

private func collectValues(_ root: TreeNode?) {
guard let root = root else { return }
treeValues.append(root.val)
collectValues(root.left)
collectValues(root.right)
}
}
Loading