题解 | #数字序列中某一位的数字#

数字序列中某一位的数字

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;
}

}

全部评论

相关推荐

03-31 18:02
门头沟学院 Java
白日梦想家_等打包版:不要的哦佛给我
点赞 评论 收藏
分享
Ncsbbss:又想干活又想要工资,怎么什么好事都让你占了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务