My solution:
First find the maximum value of n digits, for loop into the vector
class Solution {
public:
vector<int> printNumbers(int n) {
vector<int> res;
if(!n) return {};
int max=0;
while(n--){
max=max*10+9;
}
for(int i=1;i<=max;i++)
res.push_back(i);
return res;
}
};
2. Just multiply by 10 when calculating the maximum value
class Solution {
public:
vector<int> printNumbers(int n) {
vector<int> res;
if(!n) return {};
int max=1;
for(int i=0;i<n;i++) max*=10;
for(int i=1;i<max;i++)
res.push_back(i);
return res;
}
};