share my standard dfs solution!


  • 0
    T
    public class Solution {
        //char[] array=new char[]{'0','1','2','3','4','5','6','7','8','9'};
        public List<Integer> lexicalOrder(int n) {
            List<Integer> list=new ArrayList<>();
            if(n<=0) return list;
            
            for(int i=1;i<=9;i++){
                dfs(list,n,i);
            }
            return list;
        }
        
        public void dfs(List<Integer> list,int n,int num){
            if(num>n){
                return;
            }
            
            list.add(num);
            for(int i=0;i<10;i++){
                dfs(list,n,num*10+i);
            }
        }
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.