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 lcof2 problem: No.089 #3475

Merged
merged 2 commits into from
Sep 2, 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
21 changes: 21 additions & 0 deletions lcof2/剑指 Offer II 089. 房屋偷盗/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ impl Solution {
}
```

#### Swift

```swift
class Solution {
func rob(_ nums: [Int]) -> Int {
let n = nums.count
if n == 0 { return 0 }
if n == 1 { return nums[0] }

var f = Array(repeating: 0, count: n + 1)
f[1] = nums[0]

for i in 2...n {
f[i] = max(f[i - 1], f[i - 2] + nums[i - 1])
}

return f[n]
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
16 changes: 16 additions & 0 deletions lcof2/剑指 Offer II 089. 房屋偷盗/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
func rob(_ nums: [Int]) -> Int {
let n = nums.count
if n == 0 { return 0 }
if n == 1 { return nums[0] }

var f = Array(repeating: 0, count: n + 1)
f[1] = nums[0]

for i in 2...n {
f[i] = max(f[i - 1], f[i - 2] + nums[i - 1])
}

return f[n]
}
}