【剑指Offer-44】数字序列中某一位的数字
问题
数字以0123456789101112131415…的格式序列化到一个字符序列中。在这个序列中,第5位(从下标0开始计数)是5,第13位是1,第19位是4,等等。
请写一个函数,求任意第n位对应的数字。
示例
输入: n = 11
输出: 0
解答
class Solution {
public:
int findNthDigit(int n) {
int digit = 1;
long start = 1;
while (n > 9 * digit * start) {
n -= 9 * digit * start;
digit++;
start *= 10;
}
long num = start + (n - 1) / digit;
return to_string(num)[(n - 1) % digit] - '0';
}
};
重点思路
本题分三步:1.确定所求数位的所在数字的位数;2.确定所求数位所在的数字;3.确定所求数位在num的哪一数位。
![](https://pic.leetcode-cn.com/16836ca609f8b4d9af776b35eab4a4c4a86d76f4628a1bc931e56d197617bbb4-Picture2.png)
![](https://pic.leetcode-cn.com/1f2cefd22a9825eb4a52d606a4aee2f93dd659d1b332d3b6a6ed68e5289e8d01-Picture3.png)
![](https://pic.leetcode-cn.com/09af6bd37d9c79d9b904bedef01f0464aee1cd15e18d8a2ea86b70b312a830c3-Picture4.png)