【LeetCode】剑指 Offer 44. 数字序列中某一位的数字
文章目录
package offer;
public class Solution44 {
public static void main(String[] args) {
int n = 11;
Solution44 solution = new Solution44();
System.out.println(solution.method(n));
}
private int method(int n) {
int digit = 1;
long count = 9;
long start = 1;
while (n > count) {
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n - 1) / digit;
return Long.toString(num).charAt((n - 1) % digit) - '0';
}
}
//时间复杂度为 O(logn)
//空间复杂度为 O(logn)