本文主要是介绍386. Lexicographical Numbers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Given an integer n, return 1 - n in lexicographical order.
For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].
Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000.
给定一个int,以字典序返回小于这个int的所有值
从1开始 不断的深入递归,(1)-(10-(100-109)- 判断条件也很简单只要小于n就放到list中
public class Solution {public List<Integer> lexicalOrder(int n) {List<Integer> res=new ArrayList<Integer>();for(int i=1;i<=9;i++){dfs(i,n,res);}return res;}public void dfs(int cur,int n,List<Integer> res){if(cur>n) return ;res.add(cur);for(int i=0;i<=9;i++){dfs(cur*10+i,n,res);}}
}
这篇关于386. Lexicographical Numbers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!