-
Notifications
You must be signed in to change notification settings - Fork 0
/
17-打印从1到最大的n位数.py
40 lines (33 loc) · 1.07 KB
/
17-打印从1到最大的n位数.py
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 16 20:19:46 2019
@author: Zzj
"""
# 17.打印从1到最大的n位数
class Solution:
def Print1ToMaxNDigits(self, n):
if n <= 0 :
return
number = ['0'] * n
for i in range(10):
number[0] = str(i)
self.Print1ToMaxNDigitsRecursively(number, n, 0)
def Print1ToMaxNDigitsRecursively(self, number, length, index):
if index == length-1:
self.PrintNumber(number)
return
for i in range(10):
number[index+1] = str(i)
self.Print1ToMaxNDigitsRecursively(number, length, index+1)
def PrintNumber(self, number):
isBegining0 = True
nLength = len(number)
for i in range(nLength):
if isBegining0 and number[i] != '0':
isBegining0 = False
if not isBegining0:
print('%c'%number[i])
print('\t')
if __name__ == "__main__":
a = Solution()
a.Print1ToMaxNDigits(2)