forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
49 lines (49 loc) · 853 Bytes
/
Solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
func numberCount(a int, b int) int {
num := strconv.Itoa(b)
f := make([][1 << 10]int, len(num))
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
}
var dfs func(pos, mask int, limit bool) int
dfs = func(pos, mask int, limit bool) int {
if pos >= len(num) {
if mask != 0 {
return 1
}
return 0
}
if !limit && f[pos][mask] != -1 {
return f[pos][mask]
}
up := 9
if limit {
up = int(num[pos] - '0')
}
ans := 0
for i := 0; i <= up; i++ {
if mask>>i&1 == 1 {
continue
}
nxt := mask | 1<<i
if mask == 0 && i == 0 {
nxt = 0
}
ans += dfs(pos+1, nxt, limit && i == up)
}
if !limit {
f[pos][mask] = ans
}
return ans
}
y := dfs(0, 0, true)
num = strconv.Itoa(a - 1)
for i := range f {
for j := range f[i] {
f[i][j] = -1
}
}
x := dfs(0, 0, true)
return y - x
}