剑指 Offer 17. 打印从1到最大的n位数

考虑大数的情况

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
var res []int
var count int
func printNumbers(n int) []int{
res = make([]int,int(math.Pow10(n)-1))
count = 0
for digit:=1; digit<=n;digit++{
for first:='1';first<='9';first++{
num := make([]byte,digit)
num[0]=byte(first)
dfs(1,num,digit)
}
}
return res
}
func dfs(index int, num []byte, digit int){
if index == digit{
tmp,_ := strconv.Atoi(string(num))
res[count] = tmp
count ++
return
}
for i:= '0'; i<='9';i++{
num[index] = byte(i)
dfs(index+1, num, digit)
}
}