【LeetCode】剑指 Offer 44. 数字序列中某一位的数字
作者:互联网
【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)
标签:count,digit,数字,Offer,int,44,start 来源: https://blog.csdn.net/qq_45593575/article/details/122285164