题意:给出一个正整数n<2^31-1,求序列中1,2,3,4,...,的第n个数是多少。如果n=10,第10个数是0

思路:用两个数组f和init,数组f[i]表示位数为i的数的个数,init[i]表示位数为i的最小值。从下标1开始,直到n-i*f[i]小于0,此时找到第n位数所在的数有几位。然后找到在位数为i时所在的第几个数,接着确定是这个数的第几位。

代码如下:

public class Solution
{private static final int N = 15;private static long[] f;private static long[] init;static{f = new long[N];init = new long[N];f[1] = 9;init[1] = 1;for (int i = 2; i < N; i++){f[i] = f[i - 1] * 10;init[i] = init[i - 1] * 10;}}public int findNthDigit(int n){/*for (int i = 1; i < N; i++){System.out.println(f[i]);}*/int index = 1;while (n - index * f[index] > 0){n -= index * f[index];index++;}int cnt = (n + index - 1) / index - 1;int remainder = n % index;//System.out.println("index:" + index + " n:" + n + " cnt:" + cnt + " remainder:" + remainder);String str = Long.toString(init[index] + cnt);//System.out.println("str:" + str);if (remainder == 0){return Integer.parseInt(str.substring(str.length() - 1));}else{return Integer.parseInt(str.substring(remainder - 1, remainder));}}
}

LeetCode Nth Digit相关推荐

  1. C#LeetCode刷题之#400-第N个数字(Nth Digit)

    问题 在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意:n 是正数且在32为整形范围内 ( n < 231). 输入:3 ...

  2. 杭电1597 find the nth digit

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. HDU1597 find the nth digit【模拟】

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  4. HDU1597 find the nth digit

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  5. hdu1597 find the nth digit(二分查找)

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  6. hdu 1597 find the nth digit(等差求和+二分)

    find the nth digit Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  7. LeetCode算法题-Nth Digit(Java实现)

    这是悦乐书的第215次更新,第228篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第83题(顺位题号是400).找到无限整数序列的第n个数字1,2,3,4,5,6,7,8 ...

  8. leetcode 400. Nth Digit | 400. 第 N 位数字(二分法找左侧不大于n的第一个数)

    题目 https://leetcode.com/problems/nth-digit/ 题解 本题考察 Math, Binary Search.思路不难,需要扣边界细节. class Solution ...

  9. [LeetCode]400. Nth Digit

    /*思路很简单 没有很特别的方法1.先找到要找的数是几位数2.再找到要找的数是那个数中的3.最后在上一步的数中找到结果*/public int findNthDigit(int n) {//几位数lo ...

最新文章

  1. R语言关系操作符:>、<=、!=、>=、==、
  2. BZOJ 1108 [POI2007]天然气管道Gaz
  3. 深入理解多线程(三)—— Java的对象头
  4. 老板,年终奖我不要了,请给我一部华为Mate RS保时捷设计手机
  5. django-自定义转换器-实操案例
  6. Mysql数据库及帐号的权限查询
  7. 建立Socket Policy服务器
  8. react实现聊天界面_React-Redux 100行代码简易版探究原理
  9. [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.9
  10. COM组件注册DLL不成功
  11. Composer 基础使用
  12. 用Hadoop分析金庸人物关系网-实验报告
  13. ChatGPT神器免费使用,告别昂贵低效工具
  14. Vivo手机实现录音转文字的方法,图文步骤详解,特别简单
  15. 梳理正则表达式发展史
  16. 论坛APP开发需要具备哪些功能
  17. android 获取wifi信号强度,获取wifi当前手机连接的wifi信息以及信号强度
  18. 港美股券商架构最佳实践
  19. mysql数据库通过高德地图经纬度计算两点之间的距离
  20. SIMCOM7600CE内置MQTT异常

热门文章

  1. 散列表查找的一个实例
  2. IntentService
  3. asp.net select Case条件语句的使用方法
  4. 伏威谈淘宝网的高并发处理与压力测试(转)
  5. PostMessage()和SendMessage()
  6. 零基础学python看什么书-转行零基础该如何学习python?很庆幸,三年前的我选对了...
  7. python发明者叫什么-看看9种编程语言的发明者是怎么说的
  8. python安装教程windows-Python for windows 安装教程
  9. python找工作难吗-为什么我不建议你通过 Python 去找工作?
  10. python绘制动态图-Python图像处理之gif动态图的解析与合成操作详解