字符串排序

var lexicalOrder = function(n) {
    return Array(n).fill(null).map((v, i) => v = "" + (i + 1)).sort()
};

深度优先搜索

var lexicalOrder = function(n) {
    const ret = [];
    let number = 1;
    for (let i = 0; i < n; i++) {
        ret.push(number);
        if (number * 10 <= n) {
            number *= 10;
        } else {
            while (number % 10 === 9 || number + 1 > n) {
                number = Math.floor(number / 10);
            }
            number++;
        }
    }
    return ret;
};