Simple Java Solution


  • 0
    H
    public class Solution {
        public List<Integer> lexicalOrder(int n) {
            List<Integer> ans = new ArrayList<>();
            for (int i = 1; i < 10; i++)
                lexicalOrder(i, ans, n);
            return ans;
        }
        
        private void lexicalOrder(int prefix, List<Integer> ans, int n) {
            if (prefix > n) return;
            ans.add(prefix);
            int p = prefix * 10;
            for (int i = 0; i < 10; i++) {
                lexicalOrder(p + i, ans, n);
            }
        }
        
    }
    

Log in to reply
 

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