Skip to content

Commit

Permalink
一刷400
Browse files Browse the repository at this point in the history
  • Loading branch information
diguage committed Sep 21, 2024
1 parent 9f289f8 commit dd7c814
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 9 deletions.
16 changes: 8 additions & 8 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2825,14 +2825,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
//|{doc_base_url}/0399-evaluate-division.adoc[题解]
//|Medium
//|
//
//|{counter:codes}
//|{leetcode_base_url}/nth-digit/[400. Nth Digit^]
//|{source_base_url}/_0400_NthDigit.java[Java]
//|{doc_base_url}/0400-nth-digit.adoc[题解]
//|Medium
//|
//

|{counter:codes}
|{leetcode_base_url}/nth-digit/[400. Nth Digit^]
|{source_base_url}/_0400_NthDigit.java[Java]
|{doc_base_url}/0400-nth-digit.adoc[题解]
|Medium
|

//|{counter:codes}
//|{leetcode_base_url}/binary-watch/[401. Binary Watch^]
//|{source_base_url}/_0401_BinaryWatch.java[Java]
Expand Down
30 changes: 30 additions & 0 deletions docs/0400-nth-digit.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,41 @@ _n_ is positive and will fit within the range of a 32-bit signed integer (_n_ <
The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.
----

== 思路分析

image::images/0400-01.png[{image_attr}]

image::images/0400-02.png[{image_attr}]

image::images/0400-03.png[{image_attr}]

image::images/0400-04.png[{image_attr}]

image::images/0400-05.png[{image_attr}]

image::images/0400-06.png[{image_attr}]

image::images/0400-07.png[{image_attr}]

[[src-0400]]
[tabs]
====
一刷::
+
--
[{java_src_attr}]
----
include::{sourcedir}/_0400_NthDigit.java[tag=answer]
----
--
// 二刷::
// +
// --
// [{java_src_attr}]
// ----
// include::{sourcedir}/_0400_NthDigit_2.java[tag=answer]
// ----
// --
====

Binary file added docs/images/0400-01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0400-02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0400-03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0400-04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0400-05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0400-06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/0400-07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ include::0395-longest-substring-with-at-least-k-repeating-characters.adoc[levelo

// include::0399-evaluate-division.adoc[leveloffset=+1]

// include::0400-nth-digit.adoc[leveloffset=+1]
include::0400-nth-digit.adoc[leveloffset=+1]

// include::0401-binary-watch.adoc[leveloffset=+1]

Expand Down
6 changes: 6 additions & 0 deletions logbook/202406.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,12 @@
|{doc_base_url}/0768-max-chunks-to-make-sorted-ii.adoc[题解]
|❌ 贪心+单调栈。单调栈的解法思路很精巧!

|{counter:codes}
|{leetcode_base_url}/nth-digit/[400. Nth Digit^]
|{doc_base_url}/0400-nth-digit.adoc[题解]
|❌ 纯纯的数学运算,一脸懵逼


|===

截止目前,本轮练习一共完成 {codes} 道题。
36 changes: 36 additions & 0 deletions src/main/java/com/diguage/algo/leetcode/_0400_NthDigit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.diguage.algo.leetcode;

public class _0400_NthDigit {
// tag::answer[]

/**
* @author D瓜哥 · https://www.diguage.com
* @since 2024-09-21 18:58:09
*/
public int findNthDigit(int n) {
int digit = 1; // n所在数字有几位数
long start = 1; // 每digit位的起始数字,1位数从2开始,2位数从10开始..
long count = 9; // 所有digit位数的数位数量, 所有1位数有9个位,所有的2位数90x2个位..
// 1. 确定n所在的数字的位数digit
// 循环执行 n 减去 一位数、两位数、... 的数位数量 count ,直至 n≤count 时跳出。
while (n > count) {
n -= count; // n分别减去1位数 2位数 3位数的个数..
start *= 10;
digit += 1;
count = digit * start * 9;
}
// 2. 确定n所在的数字num,n-1是从0开始计算偏移量
// 所求数位在从数字 start 开始的第 [(n−1)/digit] 个 数字 中(start 为第 0 个数字)。
long num = start + (n - 1) / digit;
// 3. 确定所求数位是 num 的第几位数字,从0算起
// 所求数位为数字 num 的第 (n−1)%digit 位(数字的首个数位为第 0 位)。
// 注:上面第二步已经确定在哪个数字了,那么取余就是找在该数中的第多少位。
// 能整除的部分,已经在前面n-1个数字中了
return Long.toString(num).charAt((n - 1) % digit) - '0';
}
// end::answer[]

public static void main(String[] args) {
new _0400_NthDigit().findNthDigit(300);
}
}

0 comments on commit dd7c814

Please sign in to comment.