题解 | #打印从1到最大的n位数#
打印从1到最大的n位数
https://www.nowcoder.com/practice/4436c93e568c48f6b28ff436173b997f
#include <vector>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param n int整型 最大位数
* @return int整型vector
*/
vector<int> printNumbers(int n) {
// write code here
int a=0;
int b=1;
for(int i=0;i<n;i++){
a = a + 9*b;
b = b*10;
}
vector<int> out;
for (int j=1; j<=a; j++) {
out.push_back(j);
}
return out;
}
};
注意 对于vector, 当未提前定义vector大小时,不能直接用下标操作符直接修改元素,而是用.push_back方法在末尾填充数据
基恩士成长空间 419人发布