File tree 3 files changed +114
-0
lines changed
lcci/17.06.Number Of 2s In Range
3 files changed +114
-0
lines changed Original file line number Diff line number Diff line change @@ -193,6 +193,45 @@ func numberOf2sInRange(n int) int {
193
193
}
194
194
```
195
195
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
+
196
235
<!-- tabs:end -->
197
236
198
237
<!-- end -->
Original file line number Diff line number Diff line change @@ -196,6 +196,45 @@ func numberOf2sInRange(n int) int {
196
196
}
197
197
```
198
198
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
+
199
238
<!-- tabs:end -->
200
239
201
240
<!-- end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments