领扣LintCode算法问题答案-1256. 第n个数位
发布日期:2021-06-30 17:10:33
浏览次数:2
分类:技术文章
本文共 953 字,大约阅读时间需要 3 分钟。
领扣LintCode算法问题答案-1256. 第n个数位
目录
1256. 第n个数位
描述
找出无限正整数数列1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, …中的第n个数位。
- n是一个正整数,并且不会超出32位有符号整数的范围(n < 231)。
样例 1:
输入:3输出:3
样例 2:
输入:11输出:0解析:数字序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 第11位是0
题解
public class Solution { /** * @param n: a positive integer * @return: the nth digit of the infinite integer sequence */ public int findNthDigit(int n) { // write your code here long b = 0; do { b++; long count = b * 9 * (long) Math.pow(10, b - 1); if (n < count) { break; } n -= count; } while (n > 0); long lastNum = (int) Math.pow(10, b - 1) - 1; long num = (n + (b - 1)) / b + lastNum; String str = String.valueOf(num); char c = str.charAt((int)((n + (b - 1)) % b)); return c - 48; }}
鸣谢
非常感谢你愿意花时间阅读本文章,本人水平有限,如果有什么说的不对的地方,请指正。
欢迎各位留言讨论,希望小伙伴们都能每天进步一点点。
转载地址:https://le-yi.blog.csdn.net/article/details/108907455 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月16日 10时17分36秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
牛客国庆集训派对Day5——L 数论之神(找规律/数论)
2019-04-30
CodeForces - 629B Far Relative’s Problem(模拟)
2019-04-30
CodeForces - 1059B Forgery (思维)
2019-04-30
CodeForces - 456C Boredom (dp)
2019-04-30
CodeForces - 960B Minimize the error (思维,贪心)
2019-04-30
CodeForces - 97B Superset (思维/分治/构造)
2019-04-30
CodeForces - 675A Infinite Sequence(简单数论 细节)
2019-04-30
CodeForces - 1042B Vitamins (思维)
2019-04-30
ACM 2013 长沙区域赛 Alice's Print Service (二分 思维)
2019-04-30
ACM 2013 长沙区域赛 Collision (几何)
2019-04-30
CodeForces - 1064A Make a triangle! (简单模拟)
2019-04-30
51Nod - 1183 编辑距离 (dp)
2019-04-30
ACM 2014 鞍山区域赛 E - Hatsune Miku (dp)
2019-04-30
反向传播&梯度下降 的直观理解程序(numpy)
2019-04-30
ACM 2017 南宁区域赛 Rake it in(对抗搜索)
2019-04-30