Skip to content

Commit a3bb154

Browse files
authored
feat: add swift implementation to lcci problem: No.17.06 (#2774)
1 parent 8c59e5d commit a3bb154

File tree

3 files changed

+114
-0
lines changed

3 files changed

+114
-0
lines changed

lcci/17.06.Number Of 2s In Range/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,45 @@ func numberOf2sInRange(n int) int {
193193
}
194194
```
195195

196+
```swift
197+
class Solution {
198+
private var a = [Int](repeating: 0, count: 12)
199+
private var dp = [[Int]](repeating: [Int](repeating: -1, count: 12), count: 12)
200+
201+
func numberOf2sInRange(_ n: Int) -> Int {
202+
var n = n
203+
var len = 0
204+
while n > 0 {
205+
len += 1
206+
a[len] = n % 10
207+
n /= 10
208+
}
209+
for i in 0..<12 {
210+
dp[i] = [Int](repeating: -1, count: 12)
211+
}
212+
return dfs(len, 0, true)
213+
}
214+
215+
private func dfs(_ pos: Int, _ cnt: Int, _ limit: Bool) -> Int {
216+
if pos <= 0 {
217+
return cnt
218+
}
219+
if !limit && dp[pos][cnt] != -1 {
220+
return dp[pos][cnt]
221+
}
222+
let up = limit ? a[pos] : 9
223+
var ans = 0
224+
for i in 0...up {
225+
ans += dfs(pos - 1, cnt + (i == 2 ? 1 : 0), limit && i == up)
226+
}
227+
if !limit {
228+
dp[pos][cnt] = ans
229+
}
230+
return ans
231+
}
232+
}
233+
```
234+
196235
<!-- tabs:end -->
197236

198237
<!-- end -->

lcci/17.06.Number Of 2s In Range/README_EN.md

+39
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,45 @@ func numberOf2sInRange(n int) int {
196196
}
197197
```
198198

199+
```swift
200+
class Solution {
201+
private var a = [Int](repeating: 0, count: 12)
202+
private var dp = [[Int]](repeating: [Int](repeating: -1, count: 12), count: 12)
203+
204+
func numberOf2sInRange(_ n: Int) -> Int {
205+
var n = n
206+
var len = 0
207+
while n > 0 {
208+
len += 1
209+
a[len] = n % 10
210+
n /= 10
211+
}
212+
for i in 0..<12 {
213+
dp[i] = [Int](repeating: -1, count: 12)
214+
}
215+
return dfs(len, 0, true)
216+
}
217+
218+
private func dfs(_ pos: Int, _ cnt: Int, _ limit: Bool) -> Int {
219+
if pos <= 0 {
220+
return cnt
221+
}
222+
if !limit && dp[pos][cnt] != -1 {
223+
return dp[pos][cnt]
224+
}
225+
let up = limit ? a[pos] : 9
226+
var ans = 0
227+
for i in 0...up {
228+
ans += dfs(pos - 1, cnt + (i == 2 ? 1 : 0), limit && i == up)
229+
}
230+
if !limit {
231+
dp[pos][cnt] = ans
232+
}
233+
return ans
234+
}
235+
}
236+
```
237+
199238
<!-- tabs:end -->
200239

201240
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
private var a = [Int](repeating: 0, count: 12)
3+
private var dp = [[Int]](repeating: [Int](repeating: -1, count: 12), count: 12)
4+
5+
func numberOf2sInRange(_ n: Int) -> Int {
6+
var n = n
7+
var len = 0
8+
while n > 0 {
9+
len += 1
10+
a[len] = n % 10
11+
n /= 10
12+
}
13+
for i in 0..<12 {
14+
dp[i] = [Int](repeating: -1, count: 12)
15+
}
16+
return dfs(len, 0, true)
17+
}
18+
19+
private func dfs(_ pos: Int, _ cnt: Int, _ limit: Bool) -> Int {
20+
if pos <= 0 {
21+
return cnt
22+
}
23+
if !limit && dp[pos][cnt] != -1 {
24+
return dp[pos][cnt]
25+
}
26+
let up = limit ? a[pos] : 9
27+
var ans = 0
28+
for i in 0...up {
29+
ans += dfs(pos - 1, cnt + (i == 2 ? 1 : 0), limit && i == up)
30+
}
31+
if !limit {
32+
dp[pos][cnt] = ans
33+
}
34+
return ans
35+
}
36+
}

0 commit comments

Comments
 (0)