mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
Update 400 solution
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
# [400. Nth Digit](https://leetcode-cn.com/problems/nth-digit/)
|
||||
# [400. Nth Digit](https://leetcode.com/problems/nth-digit/)
|
||||
|
||||
## 题目
|
||||
|
||||
@ -25,25 +25,26 @@ Given an integer n, return the nth digit of the infinite integer sequence [1, 2,
|
||||
|
||||
## 解题思路
|
||||
|
||||
- bits = 1的时候有1,2,3,4,5,6,7,8,9这9个数;9 = math.Pow10(bits - 1) * bits
|
||||
- bits = 2的时候有10-99这90个数;90 = math.Pow10(bits - 1) * bits
|
||||
- n不断减去bits从1开始的数字总数,求出n所在的数字是几位数即bits
|
||||
- 计算n所在的数字num,等于初始值加上(n - 1) / bits
|
||||
- 计算n所在这个数字的第几位digitIdx等于(n - 1) % bits
|
||||
- 计算出digitIdx位的数字
|
||||
- bits = 1 的时候有 1,2,3,4,5,6,7,8,9 这 9 个数; 9 = math.Pow10(bits - 1) * bits
|
||||
- bits = 2 的时候有 10-99 这 90 个数; 90 = math.Pow10(bits - 1) * bits
|
||||
- n 不断减去 bits 从 1 开始的数字总数,求出 n 所在的数字是几位数即 bits
|
||||
- 计算 n 所在的数字 num,等于初始值加上 (n - 1) / bits
|
||||
- 计算 n 所在这个数字的第几位 digitIdx 等于 (n - 1) % bits
|
||||
- 计算出 digitIdx 位的数字
|
||||
|
||||
### 以11为例:
|
||||
### 以11 为例:
|
||||
11 - 9 = 2
|
||||
|
||||
(2 - 1) / 2 = 0
|
||||
|
||||
(2 - 1) % 2 = 1
|
||||
|
||||
也就是说第11位数字是位数是2的第一个数字的第二位,即是0
|
||||
也就是说第 11 位数字是位数是 2 的第一个数字的第二位,即是 0
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
@ -120,5 +120,5 @@ func calcEquation(equations [][]string, values []float64, queries [][]string) []
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0397.Integer-Replacement/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0401.Binary-Watch/">下一页➡️</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0400.Nth-Digit/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
74
website/content/ChapterFour/0400~0499/0400.Nth-Digit.md
Normal file
74
website/content/ChapterFour/0400~0499/0400.Nth-Digit.md
Normal file
@ -0,0 +1,74 @@
|
||||
# [400. Nth Digit](https://leetcode.com/problems/nth-digit/)
|
||||
|
||||
## 题目
|
||||
|
||||
Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...].
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input: n = 3
|
||||
Output: 3
|
||||
|
||||
**Example 2**:
|
||||
|
||||
Input: n = 11
|
||||
Output: 0
|
||||
Explanation: 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.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= n <= int(math.Pow(2, 31)) - 1
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数 n ,请你在无限的整数序列 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...] 中找出并返回第 n 位数字。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- bits = 1 的时候有 1,2,3,4,5,6,7,8,9 这 9 个数; 9 = math.Pow10(bits - 1) * bits
|
||||
- bits = 2 的时候有 10-99 这 90 个数; 90 = math.Pow10(bits - 1) * bits
|
||||
- n 不断减去 bits 从 1 开始的数字总数,求出 n 所在的数字是几位数即 bits
|
||||
- 计算 n 所在的数字 num,等于初始值加上 (n - 1) / bits
|
||||
- 计算 n 所在这个数字的第几位 digitIdx 等于 (n - 1) % bits
|
||||
- 计算出 digitIdx 位的数字
|
||||
|
||||
### 以11 为例:
|
||||
11 - 9 = 2
|
||||
|
||||
(2 - 1) / 2 = 0
|
||||
|
||||
(2 - 1) % 2 = 1
|
||||
|
||||
也就是说第 11 位数字是位数是 2 的第一个数字的第二位,即是 0
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func findNthDigit(n int) int {
|
||||
if n <= 9 {
|
||||
return n
|
||||
}
|
||||
bits := 1
|
||||
for n > 9*int(math.Pow10(bits-1))*bits {
|
||||
n -= 9 * int(math.Pow10(bits-1)) * bits
|
||||
bits++
|
||||
}
|
||||
idx := n - 1
|
||||
start := int(math.Pow10(bits - 1))
|
||||
num := start + idx/bits
|
||||
digitIdx := idx % bits
|
||||
return num / int(math.Pow10(bits-digitIdx-1)) % 10
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0399.Evaluate-Division/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0401.Binary-Watch/">下一页➡️</a></p>
|
||||
</div>
|
@ -178,6 +178,6 @@ func findReadBinaryWatchHour(target, index int, c []int, res *[]string) {
|
||||
|
||||
----------------------------------------------
|
||||
<div style="display: flex;justify-content: space-between;align-items: center;">
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0300~0399/0399.Evaluate-Division/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0400.Nth-Digit/">⬅️上一页</a></p>
|
||||
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0400~0499/0402.Remove-K-Digits/">下一页➡️</a></p>
|
||||
</div>
|
||||
|
@ -158,6 +158,7 @@ func peakIndexInMountainArray(A []int) int {
|
||||
|0367|Valid Perfect Square|[Go]({{< relref "/ChapterFour/0300~0399/0367.Valid-Perfect-Square.md" >}})|Easy||||42.7%|
|
||||
|0374|Guess Number Higher or Lower|[Go]({{< relref "/ChapterFour/0300~0399/0374.Guess-Number-Higher-or-Lower.md" >}})|Easy||||47.8%|
|
||||
|0378|Kth Smallest Element in a Sorted Matrix|[Go]({{< relref "/ChapterFour/0300~0399/0378.Kth-Smallest-Element-in-a-Sorted-Matrix.md" >}})|Medium||||58.8%|
|
||||
|0400|Nth Digit|[Go]({{< relref "/ChapterFour/0400~0499/0400.Nth-Digit.md" >}})|Medium||||33.2%|
|
||||
|0410|Split Array Largest Sum|[Go]({{< relref "/ChapterFour/0400~0499/0410.Split-Array-Largest-Sum.md" >}})|Hard||||49.1%|
|
||||
|0436|Find Right Interval|[Go]({{< relref "/ChapterFour/0400~0499/0436.Find-Right-Interval.md" >}})|Medium||||49.1%|
|
||||
|0441|Arranging Coins|[Go]({{< relref "/ChapterFour/0400~0499/0441.Arranging-Coins.md" >}})|Easy||||44.8%|
|
||||
|
@ -94,7 +94,7 @@ weight: 7
|
||||
|1143|Longest Common Subsequence|[Go]({{< relref "/ChapterFour/1100~1199/1143.Longest-Common-Subsequence.md" >}})|Medium||||58.8%|
|
||||
|1235|Maximum Profit in Job Scheduling|[Go]({{< relref "/ChapterFour/1200~1299/1235.Maximum-Profit-in-Job-Scheduling.md" >}})|Hard||||50.6%|
|
||||
|1463|Cherry Pickup II|[Go]({{< relref "/ChapterFour/1400~1499/1463.Cherry-Pickup-II.md" >}})|Hard||||68.3%|
|
||||
|1641|Count Sorted Vowel Strings|[Go]({{< relref "/ChapterFour/1600~1699/1641.Count-Sorted-Vowel-Strings.md" >}})|Medium||||74.9%|
|
||||
|1641|Count Sorted Vowel Strings|[Go]({{< relref "/ChapterFour/1600~1699/1641.Count-Sorted-Vowel-Strings.md" >}})|Medium||||74.8%|
|
||||
|1646|Get Maximum in Generated Array|[Go]({{< relref "/ChapterFour/1600~1699/1646.Get-Maximum-in-Generated-Array.md" >}})|Easy||||51.2%|
|
||||
|1653|Minimum Deletions to Make String Balanced|[Go]({{< relref "/ChapterFour/1600~1699/1653.Minimum-Deletions-to-Make-String-Balanced.md" >}})|Medium||||54.4%|
|
||||
|1654|Minimum Jumps to Reach Home|[Go]({{< relref "/ChapterFour/1600~1699/1654.Minimum-Jumps-to-Reach-Home.md" >}})|Medium||||25.8%|
|
||||
|
@ -61,7 +61,7 @@ weight: 4
|
||||
|1019|Next Greater Node In Linked List|[Go]({{< relref "/ChapterFour/1000~1099/1019.Next-Greater-Node-In-Linked-List.md" >}})|Medium| O(n)| O(1)||59.3%|
|
||||
|1171|Remove Zero Sum Consecutive Nodes from Linked List|[Go]({{< relref "/ChapterFour/1100~1199/1171.Remove-Zero-Sum-Consecutive-Nodes-from-Linked-List.md" >}})|Medium||||42.1%|
|
||||
|1290|Convert Binary Number in a Linked List to Integer|[Go]({{< relref "/ChapterFour/1200~1299/1290.Convert-Binary-Number-in-a-Linked-List-to-Integer.md" >}})|Easy||||82.8%|
|
||||
|1669|Merge In Between Linked Lists|[Go]({{< relref "/ChapterFour/1600~1699/1669.Merge-In-Between-Linked-Lists.md" >}})|Medium||||74.6%|
|
||||
|1669|Merge In Between Linked Lists|[Go]({{< relref "/ChapterFour/1600~1699/1669.Merge-In-Between-Linked-Lists.md" >}})|Medium||||74.7%|
|
||||
|1670|Design Front Middle Back Queue|[Go]({{< relref "/ChapterFour/1600~1699/1670.Design-Front-Middle-Back-Queue.md" >}})|Medium||||55.2%|
|
||||
|1721|Swapping Nodes in a Linked List|[Go]({{< relref "/ChapterFour/1700~1799/1721.Swapping-Nodes-in-a-Linked-List.md" >}})|Medium||||65.8%|
|
||||
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|-------------|-------------|
|
||||
|
@ -52,6 +52,7 @@ weight: 12
|
||||
|0371|Sum of Two Integers|[Go]({{< relref "/ChapterFour/0300~0399/0371.Sum-of-Two-Integers.md" >}})|Medium||||50.6%|
|
||||
|0372|Super Pow|[Go]({{< relref "/ChapterFour/0300~0399/0372.Super-Pow.md" >}})|Medium||||37.6%|
|
||||
|0384|Shuffle an Array|[Go]({{< relref "/ChapterFour/0300~0399/0384.Shuffle-an-Array.md" >}})|Medium||||56.2%|
|
||||
|0400|Nth Digit|[Go]({{< relref "/ChapterFour/0400~0499/0400.Nth-Digit.md" >}})|Medium||||33.2%|
|
||||
|0405|Convert a Number to Hexadecimal|[Go]({{< relref "/ChapterFour/0400~0499/0405.Convert-a-Number-to-Hexadecimal.md" >}})|Easy||||45.4%|
|
||||
|0412|Fizz Buzz|[Go]({{< relref "/ChapterFour/0400~0499/0412.Fizz-Buzz.md" >}})|Easy||||65.6%|
|
||||
|0423|Reconstruct Original Digits from English|[Go]({{< relref "/ChapterFour/0400~0499/0423.Reconstruct-Original-Digits-from-English.md" >}})|Medium||||51.4%|
|
||||
|
@ -65,7 +65,7 @@ weight: 5
|
||||
|1006|Clumsy Factorial|[Go]({{< relref "/ChapterFour/1000~1099/1006.Clumsy-Factorial.md" >}})|Medium||||54.3%|
|
||||
|1019|Next Greater Node In Linked List|[Go]({{< relref "/ChapterFour/1000~1099/1019.Next-Greater-Node-In-Linked-List.md" >}})|Medium| O(n)| O(1)||59.3%|
|
||||
|1021|Remove Outermost Parentheses|[Go]({{< relref "/ChapterFour/1000~1099/1021.Remove-Outermost-Parentheses.md" >}})|Easy| O(n)| O(1)||79.6%|
|
||||
|1047|Remove All Adjacent Duplicates In String|[Go]({{< relref "/ChapterFour/1000~1099/1047.Remove-All-Adjacent-Duplicates-In-String.md" >}})|Easy| O(n)| O(1)||71.2%|
|
||||
|1047|Remove All Adjacent Duplicates In String|[Go]({{< relref "/ChapterFour/1000~1099/1047.Remove-All-Adjacent-Duplicates-In-String.md" >}})|Easy| O(n)| O(1)||71.1%|
|
||||
|1111|Maximum Nesting Depth of Two Valid Parentheses Strings|[Go]({{< relref "/ChapterFour/1100~1199/1111.Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings.md" >}})|Medium||||72.5%|
|
||||
|1190|Reverse Substrings Between Each Pair of Parentheses|[Go]({{< relref "/ChapterFour/1100~1199/1190.Reverse-Substrings-Between-Each-Pair-of-Parentheses.md" >}})|Medium||||65.4%|
|
||||
|1209|Remove All Adjacent Duplicates in String II|[Go]({{< relref "/ChapterFour/1200~1299/1209.Remove-All-Adjacent-Duplicates-in-String-II.md" >}})|Medium||||56.1%|
|
||||
|
@ -137,7 +137,7 @@ weight: 2
|
||||
|1003|Check If Word Is Valid After Substitutions|[Go]({{< relref "/ChapterFour/1000~1099/1003.Check-If-Word-Is-Valid-After-Substitutions.md" >}})|Medium| O(n)| O(1)||57.5%|
|
||||
|1021|Remove Outermost Parentheses|[Go]({{< relref "/ChapterFour/1000~1099/1021.Remove-Outermost-Parentheses.md" >}})|Easy||||79.6%|
|
||||
|1028|Recover a Tree From Preorder Traversal|[Go]({{< relref "/ChapterFour/1000~1099/1028.Recover-a-Tree-From-Preorder-Traversal.md" >}})|Hard||||72.0%|
|
||||
|1047|Remove All Adjacent Duplicates In String|[Go]({{< relref "/ChapterFour/1000~1099/1047.Remove-All-Adjacent-Duplicates-In-String.md" >}})|Easy||||71.2%|
|
||||
|1047|Remove All Adjacent Duplicates In String|[Go]({{< relref "/ChapterFour/1000~1099/1047.Remove-All-Adjacent-Duplicates-In-String.md" >}})|Easy||||71.1%|
|
||||
|1048|Longest String Chain|[Go]({{< relref "/ChapterFour/1000~1099/1048.Longest-String-Chain.md" >}})|Medium||||57.3%|
|
||||
|1078|Occurrences After Bigram|[Go]({{< relref "/ChapterFour/1000~1099/1078.Occurrences-After-Bigram.md" >}})|Easy||||64.3%|
|
||||
|1079|Letter Tile Possibilities|[Go]({{< relref "/ChapterFour/1000~1099/1079.Letter-Tile-Possibilities.md" >}})|Medium||||76.1%|
|
||||
|
Reference in New Issue
Block a user