diff --git "a/Index/\346\225\260\345\255\246.md" "b/Index/\346\225\260\345\255\246.md" index 2cbb6d15..07b151dc 100644 --- "a/Index/\346\225\260\345\255\246.md" +++ "b/Index/\346\225\260\345\255\246.md" @@ -47,6 +47,7 @@ | [1310. 子数组异或查询](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-z-rcgu/) | 中等 | 🤩🤩🤩🤩 | | [1342. 将数字变成 0 的操作次数](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-steps-to-reduce-a-number-to-zero/solution/gong-shui-san-xie-note-bie-pian-yi-ti-sh-85fb/) | 简单 | 🤩🤩🤩🤩 | | [1442. 形成两个异或相等数组的三元组数目](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/gong-shui-san-xie-xiang-jie-shi-yong-qia-7gzm/) | 中等 | 🤩🤩🤩 | +| [1447. 最简分数](https://leetcode-cn.com/problems/simplified-fractions/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/simplified-fractions/solution/gong-shui-san-xie-jian-dan-shu-lun-yun-y-wma5/) | 中等 | 🤩🤩🤩🤩🤩 | | [1486. 数组异或操作](https://leetcode-cn.com/problems/xor-operation-in-an-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-operation-in-an-array/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-dggg/) | 简单 | 🤩🤩🤩 | | [1518. 换酒问题](https://leetcode-cn.com/problems/water-bottles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/water-bottles/solution/gong-shui-san-xie-yi-ti-shuang-jie-ji-sh-7yyo/) | 简单 | 🤩🤩🤩🤩 | | [1588. 所有奇数长度子数组的和](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-18jq3/) | 简单 | 🤩🤩🤩🤩 | diff --git "a/LeetCode/1441-1450/1447. \346\234\200\347\256\200\345\210\206\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" "b/LeetCode/1441-1450/1447. \346\234\200\347\256\200\345\210\206\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" new file mode 100644 index 00000000..06cc64b7 --- /dev/null +++ "b/LeetCode/1441-1450/1447. \346\234\200\347\256\200\345\210\206\346\225\260\357\274\210\344\270\255\347\255\211\357\274\211.md" @@ -0,0 +1,127 @@ +### 题目描述 + +这是 LeetCode 上的 **[1447. 最简分数](https://leetcode-cn.com/problems/simplified-fractions/solution/gong-shui-san-xie-jian-dan-shu-lun-yun-y-wma5/)** ,难度为 **中等**。 + +Tag : 「数学」、「最大公约数」 + + + +给你一个整数 `n` ,请你返回所有 $0$ 到 $1$ 之间(不包括 $0$ 和 $1$)满足分母小于等于  `n` 的 最简 分数 。分数可以以 **任意** 顺序返回。 + +示例 1: +``` +输入:n = 2 + +输出:["1/2"] + +解释:"1/2" 是唯一一个分母小于等于 2 的最简分数。 +``` +示例 2: +``` +输入:n = 3 + +输出:["1/2","1/3","2/3"] +``` +示例 3: +``` +输入:n = 4 + +输出:["1/2","1/3","1/4","2/3","3/4"] + +解释:"2/4" 不是最简分数,因为它可以化简为 "1/2" 。 +``` +示例 4: +``` +输入:n = 1 + +输出:[] +``` + +提示: +* $1 <= n <= 100$ + +--- + +### 数论 + +数据范围为 $100$ 且数值大小在 $(0, 1)$ 之间,因此枚举「分子 + 分母」的 $O(n^2)$ 做法是可接受的。 + +于是问题转化为:**如何快速判断两个数组成的分数是否为最简(即判断两个数的最大公约数是否为 $1$)。** + +快速求得 $a$ 和 $b$ 的最大公约数的主要方式有两种 :「更相减损法」和「欧几里得算法」,其中「欧几里得算法」的递归实现最为好写,复杂度为 $O(\log{(a + b)})$,在绝大多数的情况下适用,只有在需要实现高精度时,才会考虑使用「更相减损法」。 + +而 stein 算法则是没有必要掌握的。 + +代码: +```Java +class Solution { + int gcd(int a, int b) { // 欧几里得算法 + return b == 0 ? a : gcd(b, a % b); + } + public List simplifiedFractions(int n) { + List ans = new ArrayList<>(); + for (int i = 1; i < n; i++) { + for (int j = i + 1; j <= n; j++) { + if (gcd(i, j) == 1) ans.add(i + "/" + j); + } + } + return ans; + } +} +``` +- +```Java +class Solution { + int gcd(int a, int b) { // 更相减损法 + while (true) { + if (a > b) a -= b; + else if (a < b) b -= a; + else return a; + } + } + public List simplifiedFractions(int n) { + List ans = new ArrayList<>(); + for (int i = 1; i < n; i++) { + for (int j = i + 1; j <= n; j++) { + if (gcd(i, j) == 1) ans.add(i + "/" + j); + } + } + return ans; + } +} +``` +- +```Java +class Solution { + int gcd(int a, int b) { // stein + if (a == 0 || b == 0) return Math.max(a, b); + if (a % 2 == 0 && b % 2 == 0) return 2 * gcd(a >> 1, b >> 1); + else if (a % 2 == 0) return gcd(a >> 1, b); + else if (b % 2 == 0) return gcd(a, b >> 1); + else return gcd(Math.abs(a - b), Math.min(a, b)); + } + public List simplifiedFractions(int n) { + List ans = new ArrayList<>(); + for (int i = 1; i < n; i++) { + for (int j = i + 1; j <= n; j++) { + if (gcd(i, j) == 1) ans.add(i + "/" + j); + } + } + return ans; + } +} +``` +* 时间复杂度:枚举分子分母的复杂度为 $O(n^2)$;判断两数是否能凑成最简分数复杂度为 $O(\log{n})$。整体复杂度为 $O(n^2 * \log{n})$ +* 空间复杂度:忽略递归带来的额外空间开销,复杂度为 $O(1)$ + +--- + +### 最后 + +这是我们「刷穿 LeetCode」系列文章的第 `No.1447` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。 + +在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。 + +为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode 。 + +在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。