87% Concise Recursive Solution Java


  • 0
    O
    public class Solution {
        public List<Integer> lexicalOrder(int n) {
            List<Integer> l = new LinkedList();
            addNums(0, n, l);
            l.remove(0);
            return l;
        }
        
        private void addNums(int offset, int n, List<Integer> l) {
            for (int i = offset; i < offset + 10 && i <= n; i++) {
                l.add(i);
                if (i > 0 && i * 10 <= n)
                    addNums(i * 10, n, l);
            }
        }
    }
    

Log in to reply
 

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