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.17.06 #2774

Merged
merged 1 commit into from
May 9, 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
39 changes: 39 additions & 0 deletions lcci/17.06.Number Of 2s In Range/README.md
Original file line number Diff line number Diff line change
@@ -193,6 +193,45 @@ func numberOf2sInRange(n int) int {
}
```

```swift
class Solution {
private var a = [Int](repeating: 0, count: 12)
private var dp = [[Int]](repeating: [Int](repeating: -1, count: 12), count: 12)

func numberOf2sInRange(_ n: Int) -> Int {
var n = n
var len = 0
while n > 0 {
len += 1
a[len] = n % 10
n /= 10
}
for i in 0..<12 {
dp[i] = [Int](repeating: -1, count: 12)
}
return dfs(len, 0, true)
}

private func dfs(_ pos: Int, _ cnt: Int, _ limit: Bool) -> Int {
if pos <= 0 {
return cnt
}
if !limit && dp[pos][cnt] != -1 {
return dp[pos][cnt]
}
let up = limit ? a[pos] : 9
var ans = 0
for i in 0...up {
ans += dfs(pos - 1, cnt + (i == 2 ? 1 : 0), limit && i == up)
}
if !limit {
dp[pos][cnt] = ans
}
return ans
}
}
```

<!-- tabs:end -->

<!-- end -->
39 changes: 39 additions & 0 deletions lcci/17.06.Number Of 2s In Range/README_EN.md
Original file line number Diff line number Diff line change
@@ -196,6 +196,45 @@ func numberOf2sInRange(n int) int {
}
```

```swift
class Solution {
private var a = [Int](repeating: 0, count: 12)
private var dp = [[Int]](repeating: [Int](repeating: -1, count: 12), count: 12)

func numberOf2sInRange(_ n: Int) -> Int {
var n = n
var len = 0
while n > 0 {
len += 1
a[len] = n % 10
n /= 10
}
for i in 0..<12 {
dp[i] = [Int](repeating: -1, count: 12)
}
return dfs(len, 0, true)
}

private func dfs(_ pos: Int, _ cnt: Int, _ limit: Bool) -> Int {
if pos <= 0 {
return cnt
}
if !limit && dp[pos][cnt] != -1 {
return dp[pos][cnt]
}
let up = limit ? a[pos] : 9
var ans = 0
for i in 0...up {
ans += dfs(pos - 1, cnt + (i == 2 ? 1 : 0), limit && i == up)
}
if !limit {
dp[pos][cnt] = ans
}
return ans
}
}
```

<!-- tabs:end -->

<!-- end -->
36 changes: 36 additions & 0 deletions lcci/17.06.Number Of 2s In Range/Solution.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
private var a = [Int](repeating: 0, count: 12)
private var dp = [[Int]](repeating: [Int](repeating: -1, count: 12), count: 12)

func numberOf2sInRange(_ n: Int) -> Int {
var n = n
var len = 0
while n > 0 {
len += 1
a[len] = n % 10
n /= 10
}
for i in 0..<12 {
dp[i] = [Int](repeating: -1, count: 12)
}
return dfs(len, 0, true)
}

private func dfs(_ pos: Int, _ cnt: Int, _ limit: Bool) -> Int {
if pos <= 0 {
return cnt
}
if !limit && dp[pos][cnt] != -1 {
return dp[pos][cnt]
}
let up = limit ? a[pos] : 9
var ans = 0
for i in 0...up {
ans += dfs(pos - 1, cnt + (i == 2 ? 1 : 0), limit && i == up)
}
if !limit {
dp[pos][cnt] = ans
}
return ans
}
}