题解 | #打印从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 vector<int> res; int end = 1; // n + 1 的最小值 for (int i = 1; i <= n; ++i) { end *= 10; } for (int i = 1; i < end; ++i ) { res.push_back(i); } return res; } };
这道题的解法其实是偏离了 剑指offer的考察点;
这道题是要考察如何处理"大数"的能力(int无法表达)
但是由于要返回int, 所以就这么写了; 难度其实是降低了
note_coding 文章被收录于专栏
记录自己的解题思路, 欢迎评价