From b00057e39f829451323a86fb22610ac4d13a321b Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 15 Aug 2021 19:53:20 +0800 Subject: [PATCH] =?UTF-8?q?400:=E7=AC=AC=20N=20=E4=BD=8D=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leetcode/editor/cn/NthDigit.java | 52 +++++++++++++++++++ src/main/java/leetcode/editor/cn/NthDigit.md | 23 ++++++++ 2 files changed, 75 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/NthDigit.java create mode 100644 src/main/java/leetcode/editor/cn/NthDigit.md diff --git a/src/main/java/leetcode/editor/cn/NthDigit.java b/src/main/java/leetcode/editor/cn/NthDigit.java new file mode 100644 index 0000000..81c2d68 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/NthDigit.java @@ -0,0 +1,52 @@ +//在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 位数字。 +// +// +// +// 注意:n 是正数且在 32 位整数范围内(n < 231)。 +// +// +// +// 示例 1: +// +// +//输入:3 +//输出:3 +// +// +// 示例 2: +// +// +//输入:11 +//输出:0 +//解释:第 11 位数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是 0 ,它是 10 的一部分。 +// +// Related Topics 数学 二分查找 +// 👍 170 👎 0 + +package leetcode.editor.cn; +//400:第 N 位数字 +class NthDigit{ + public static void main(String[] args) { + //测试代码 + Solution solution = new NthDigit().new Solution(); + } + + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int findNthDigit(int n) { + int[] ant = new int[]{0, 10, 190, 2890, 38890, 488890, 5888890, 68888890, 788888890}; + int[] numBegin = new int[]{0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000}; + for(int i = ant.length - 1; i >= 0; i--){ + if(n >= ant[i]){ + int num = (n - ant[i]) / (i + 1) + numBegin[i]; + int posi = (n - ant[i]) % (i + 1); + return num % (int)Math.pow(10, i + 1 - posi) / (int)Math.pow(10, i - posi); + } + } + return 0; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/NthDigit.md b/src/main/java/leetcode/editor/cn/NthDigit.md new file mode 100644 index 0000000..4896cba --- /dev/null +++ b/src/main/java/leetcode/editor/cn/NthDigit.md @@ -0,0 +1,23 @@ +

在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 位数字。

+ +

 

+ +

注意:n 是正数且在 32 位整数范围内(n < 231)。

+ +

 

+ +

示例 1:

+ +
+输入:3
+输出:3
+
+ +

示例 2:

+ +
+输入:11
+输出:0
+解释:第 11 位数字在序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... 里是 0 ,它是 10 的一部分。
+
+
Related Topics
  • 数学
  • 二分查找
  • \n
  • 👍 170
  • 👎 0
  • \ No newline at end of file