剑指OFFER----面试题44. 数字序列中某一位的数字
链接:https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
思路:
1.确定是几位数
2.确定是第几个数
3.确定是第几位
代码:
class Solution { public: int findNthDigit(int n) { long long i = 1, s = 9, base = 1; while (n > i * s) { n -= i * s; i++; s *= 10; base *= 10; } int number = base + (n + i - 1) / i - 1 ; int r = n % i ? n % i : i; for (int j = 0; j < i - r; j++) number /= 10; return number % 10; } };