题解 | #数字序列中某一位的数字#
数字序列中某一位的数字
http://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5
import java.util.*;
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 */ public int findNthDigit (int n) { if(n == 0) return 0;
long bottom = 1L;
long top = 9L;
long wei_bottom = bottom;
long wei_top = top;
int weishu = 1;
if(n<=top && n>=bottom) {
return n;
}else {
while(n > wei_top) {
weishu++;
bottom *= 10;
top = top*10 + 9;
wei_bottom = wei_top+1;
wei_top = (top-bottom+1) * weishu + wei_bottom - 1;
}
if(n < wei_top) {
long num = (n - wei_bottom) / weishu + bottom;
long yushu = (n - wei_bottom) % weishu;
int i = weishu - 1;
for(;i != yushu;i--) num = num / 10;
while(num >= 10) num = num % 10;
return (int)num;
}
}
return 0;
}
}