<> Interview questions 17. Print from 1 To the largest n digit
Enter number n, Print out from 1 To the largest n Decimal number . For example, input 3, Then print out 1,2,3 Up to the biggest 3 digit 999.
<> Example 1:
input : n = 1
output : [1,2,3,4,5,6,7,8,9]
<> Explain :
Instead of printing, return a list of integers
n Is a positive integer
<> Solving problems 1
Over and over again , The idea is very simple as follows ( But after that , Read the explanation , It is said that the title in the original book is not so simple , It's a big number problem ):
1, Splicing n individual 9
2,new One n individual 9 Array of
3, Traverse once , Insert data
class Solution { static public int[] printNumbers(int n) { String str = ""; for
(int i = 0; i < n; i++) { str = str +"9"; } int[] ints = new int[Integer.valueOf
(str)]; for (int i = 0; i < ints.length; i++) { ints[i] = i+1; } return ints; }
public static void main(String[] args) { int[] ints = printNumbers(3); for (int
i= 0; i < ints.length; i++) { System.out.print(ints[i]+","); } } }
Technology
Daily Recommendation