最简单的方法,找到最大的数,然后循环生成数组即可

python

class Solution:
    def printNumbers(self , n: int) -> List[int]:
        max_num = int('9' * n)  //直接拼出来,转int即可
        return [i for i in range(1, max_num+1)]

c++

class Solution {
public:
    vector<int> printNumbers(int n) {
        float max_num = pow(10.0, (float)n);  //10的n次方再减1就是n位数的最大数
        vector<int> res;
        for(int i=1; i<max_num; i++){      //循环放进容器即可
            res.emplace_back(i);
        }
        return res;
    }
};