Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report