思路:DFS遍历字典树
class Solution:
# 维护了一个隐性的字典树,只要对其进行dfs即可
def lexicalOrder(self, n: int) -> List[int]:
recVal = list()
def dfs(curVal: int, curDepth: int) -> None:
if curVal > n or (curVal == 0 and curDepth != 0):
return
if curDepth > 0:
recVal.append(curVal)
for i in range(10):
dfs(curVal * 10 + i, curDepth + 1)
dfs(0, 0)
return recVal