topic:
Enter the number n, and print out in order from 1 to the largest n-digit decimal number. For example, input 3, then 1, 2, 3 will be printed out up to the maximum 3 digits 999.
Example 1:
Input: n = 1
Output: [1,2,3,4,5,6,7,8,9]
Description:
- Instead of printing, return a list of integers
- n is a positive integer
Code:
class Solution {
public int[] printNumbers(int n) {
int[] out=new int[(int)(Math.pow(10,n))-1];
for(int i=1;i<(Math.pow(10,n));i++){
out[i-1]=i;
}
return out;
}
}
Friends who don’t know the square root can see here: rounding, random number, square root, power, π, natural constant