Lexicographical Numbers using java


  • 3
    L
    public static List<Integer> lexicalOrder(int n) {
            List<Integer> list = new ArrayList<>();
            dfs(0, n, list);
            return list;
        }
    
        public static void dfs(int cur, int n, List<Integer> list) {
            for (int i = 0; i <= 9; i++) {
                int num = cur * 10 + i;
                if (num == 0) continue;
                if (num > n) return;
                list.add(num);
                dfs(num, n, list);
            }
        }
    

Log in to reply
 

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