1447. 最简分数

题目链接:https://leetcode-cn.com/problems/simplified-fractions/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public List<String> simplifiedFractions(int n) {
List<String> list = new LinkedList<>();
if (n == 1) {
return list;
}
for (int i = 1; i < n; i++) {
for (int j = i+1; j <= n; j++) {
if (gcd(i,j) == 1) {
list.add(i+"/"+j);
}
}
}
return list;
}
public int gcd(int a, int b) {
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
}

1447. 最简分数
https://pisces34.github.io/2022/02/11/leetcode/1447/
发布于
2022年2月11日
许可协议