Java O(n) non-recursive solution


  • 0
    G

    Java O(n) non-recursive solution

    public List<Integer> lexicalOrder(int n) {
            List<Integer> res = new ArrayList<>();
            if(n <= 0) return res;
            int num = 1;
            do{
                res.add(num);
                if(num * 10 <= n){
                    num *= 10;
                    continue;
                }
                while(num%10 == 9 || num == n){
                    num /= 10;
                }
                num++;
            }while(num != 1);
            return res;
        }
    

Log in to reply
 

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