Java Easy DFS Solution


  • 0
    Z
    public class Solution {
        List<Integer> res;
        public List<Integer> lexicalOrder(int n) {
            res = new ArrayList<>();   
            dfs(0,n);
            return res;
            
        }
        public void dfs(int num, int n){
            if(num>n) return;
            for(int i = num;i<=n&&i<num+10;i++){
                if(i == 0) continue;
                res.add(i);
                dfs(i*10, n);
            }
        }
    }

Log in to reply
 

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