https://leetcode-cn.com/problems/nth-digit/
中等题 不是很难 但是需要一定的思考
长度为1的数 是1-9 一共9个数 总长度位9*1=9
长度为2的数 是11-99 一共90个数 总长度为90*2=180
长度为3的数 是100-999 一共900个数 总长度为900*3=2700
以此类推 给定n 开始以此减去1 2 3对应数字的总长度 最后得到的数字就是从长度为x的第一个数开始 往后数(n-1)/x个 具体的数就是%
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
class Solution
{
public:
int findNthDigit(int n)
{
int base=1;
int t=1;
while(n){
if(n-9*base*t<=0){
break;
}
n-=9*base*t;
base*=10;
t++;
}
int a=(n-1)/t;
int b=(n-1)%t;
int ans=base+a;
string s=to_string(ans);
return s[b]-'0';
}
};







Comments | NOTHING