Self-explanatory Java solution


  • 0
    X

    I think the following code is self-explanatory.

    public List<Integer> lexicalOrder(int n) {
            List<Integer> res = new ArrayList<>();
            for (int i = 1, count = 0, limit = n / 10; count < n; count++) {
                res.add(i);
                if (i <= limit) { // append 0 when possible
                    i *= 10;
                } else {
                    if (i == n) {
                        i /= 10;
                    }
                    // remove trailing 0's
                    for (++i; i % 10 == 0; i /= 10) {}
                }
            }
            return res;
        }
    

Log in to reply
 

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