Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to solve the problem of printing n-digits from 1 to maximum by LeetCode

Shulou Source: shulou.com Published: 2022-06-01 13:07:33 09月25日 Update

Editor to share with you LeetCode how to solve the printing from 1 to the largest n-digit problem, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

Title

Enter the number n and print out the n-digit decimal number from 1 to the largest n digits in order. For example, enter 3, then print out 1, 2, 3 all the way up to the maximum 3 digits 999.

Example 1: input: n = 1 output: [1 public int 2, int 3, 4, 5, 6, 7, 8, 9] description: return a list of integers instead of printing n as a positive integer code class Solution {public int [] printNumbers (int n) {int num = (int) Math.pow (10 num)-1; int [] res = new int [num]; for (int I = 0; I < num) ITunes +) {res [I] = iTun1;} return res;}}

Large number printing

Class Solution {int [] res; int nine = 0, count = 0, start, n; char [] num, loop = {'0,1,' 2, 3, 4, 5, 6, 7, 8, 9'}; public int [] printNumbers (int n) {this.n = n Res = new int [(int) Math.pow (10, n)-1]; num = new char [n]; start = n-1; dfs (0); return res;} void dfs (int x) {if (x = n) {String s = String.valueOf (num) .substring (start) If (! s.equals ("0")) res [count++] = Integer.parseInt (s); if (n-start = = nine) start--; return;} for (char I: loop) {if (I = ='9') nine++; num [x] = I; dfs (x + 1);} nine-- }} these are all the contents of the article "how to solve the problem of printing n digits from 1 to the largest number in LeetCode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Maximum digits articles input questions content integers not much code decimal large numbers most numbers more knowledge examples industry information information channels order Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL Xiaomi vpn MariaDB