Share My Java O(n) Time Solution


  • 0
    D
        public List<Integer> lexicalOrder(int n) {
            List<Integer> list = new ArrayList<Integer>();
            int a = 1;
            for(int i = 1; i <= n; i++){
                list.add(a);
                if(a*10 <= n) a *= 10;
                else{
                    a++;
                    if(a > n) a = n/10 + 1;
                    while(a % 10 == 0) a /= 10;
                }
            }
            return list;
        }
    }

Log in to reply
 

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