forked from itcharge/AlgoNote
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] main from itcharge:main #127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create 1447. 最简分数.md
- Loading branch information
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
Solutions/1447. 最简分数.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# [1447. 最简分数](https://leetcode.cn/problems/simplified-fractions/) | ||
|
||
- 标签:数学、字符串、数论 | ||
- 难度:中等 | ||
|
||
## 题目大意 | ||
|
||
**描述**:给定一个整数 $n$。 | ||
|
||
**要求**:返回所有 0ドル$ 到 1ドル$ 之间(不包括 0ドル$ 和 1ドル$)满足分母小于等于 $n$ 的最简分数。分数可以以任意顺序返回。 | ||
|
||
**说明**: | ||
|
||
- 1ドル \le n \le 100$。 | ||
|
||
**示例**: | ||
|
||
- 示例 1: | ||
|
||
```Python | ||
输入:n = 2 | ||
输出:["1/2"] | ||
解释:"1/2" 是唯一一个分母小于等于 2 的最简分数。 | ||
``` | ||
|
||
- 示例 2: | ||
|
||
```Python | ||
输入:n = 4 | ||
输出:["1/2","1/3","1/4","2/3","3/4"] | ||
解释:"2/4" 不是最简分数,因为它可以化简为 "1/2"。 | ||
``` | ||
|
||
## 解题思路 | ||
|
||
### 思路 1:数学 | ||
|
||
如果分子和分母的最大公约数为 1ドル$ 时,则当前分数为最简分数。 | ||
|
||
而 $n$ 的数据范围为 $(1, 100)$。因此我们可以使用两重遍历,分别枚举分子和分母,然后通过判断分子和分母是否为最大公约数,来确定当前分数是否为最简分数。 | ||
|
||
### 思路 1:代码 | ||
|
||
```Python | ||
class Solution: | ||
def simplifiedFractions(self, n: int) -> List[str]: | ||
res = [] | ||
|
||
for i in range(1, n): | ||
for j in range(i + 1, n + 1): | ||
if math.gcd(i, j) == 1: | ||
res.append(str(i) + "/" + str(j)) | ||
|
||
return res | ||
``` | ||
|
||
### 思路 1:复杂度分析 | ||
|
||
- **时间复杂度**:$O(n^2 \times \log n)$。 | ||
- **空间复杂度**:$O(1)$。 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.