題目
輸入數字 n,按順序打印出從 1 到最大的 n 位十進制數。比如輸入 3,則打印出 1、2、3 一直到最大的 3 位數 999。
示例 1:
輸入: n = 1
輸出: [1,2,3,4,5,6,7,8,9]
我的解法
- 先來一下暴力破解,還沒有for循環搞不定的事??
- N*10 -1 不就是最後以爲數字了
- 然後我們再便利 N*10 -1次,將所有的數據存入數組中
- 但是這塊的得注意一下就是他是從0位開始但是第一個存儲的數據是1.所以是b+1了。
- 時間複雜度爲O(n)
- 雙百擊殺!!!!!!意料之外
class Solution {
public int[] printNumbers(int n) {
int count = 1;
for(int i = 0; i<n; i++){
count*=10 ;
}
count = count -1;
int[] result = new int[count];
for(int b = 0; b<= count-1;b++){
result[b] = b+1;
}
return result;
}
}
原題
https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/solution/javajian-zhi-offeryuan-ti-jie-fa-by-toheng-2/