400. 第N个数字

 1 // nums[0] = 9;                 // 1-9
 2 // nums[1] = 90*2;              // 10-99
 3 // nums[2] = 900*3;             // 100-999
 4 // nums[3] = 9000*4;            // 1000-9999
 5 // nums[4] = 90000*5;           // 10000-99999
 6 // nums[5] = 900000*6;          // 100000-999999
 7 // nums[6] = 9000000*7;         // 1000000-9999999
 8 // nums[7] = 90000000*8;        // 10000000-99999999
 9 // nums[8] = 900000000*9;       // 100000000-999999999
10 // nums[9] = 1147483648*10;     // 1000000000-INT_MAX
11 
12 // 找规律
13 class Solution 
14 {
15 public:
16     int findNthDigit(int n) 
17     {
18         int i = 0;
19         if(n < 10) return n;
20         while(n)
21         {
22             long long temp = 9 * pow(10,i) * (i + 1);
23             if(n < temp) break;
24             n -= temp;
25             i ++;
26         }
27         int end = pow(10,i) + (n - 1) / (i + 1);
28         int index = (n - 1) % (i + 1);
29         string str = to_string(end);
30         return str[index] - '0';
31     }
32 };

 

posted @ 2020-04-29 16:58  Jinxiaobo0509  阅读(121)  评论(0)    收藏  举报