题目链接
题目描述
实现代码
字典序概念:按照字符串比较的形式进行排序,从前往后每个位置的数字的ASCII编码进行比较
DFS算法思想:start=1,从start开始进行深度递归遍历,每次乘以10,如果小于等于n,则添加到结果列表中;如果大于n,则回到上一步start进行自增1,再按照前面的步骤进行DFS遍历,直到n个数全部字典序排列;
实现代码:
class Solution {
public List<Integer> lexicalOrder(int n) {
int start = 1;
List<Integer> resultList = new ArrayList();
for(int i=1; i<=n; i++) {
resultList.add(start);
if(start * 10 <= n) {
start *= 10;
} else {
while(start+1 > n || start % 10 == 9) {
start /= 10;
}
start++;
}
}
return resultList;
}
}