[LeetCode] Simplified Fractions

1447. Simplified Fractions

Given an integer n, return a list of all simplified fractions between 0 and 1 (exclusive) such that the denominator is less-than-or-equal-to n. You can return the answer in any order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
vector<string> simplifiedFractions(int n) {
int vis[111][111] = {};
vector<string> res;
for(int den = 2; den <= n; den++) {
for(int num = 1; num < den; num++) {
int gcd = __gcd(den, num);
if(vis[num/gcd][den/gcd]) continue;
vis[num/gcd][den/gcd] = true;
res.push_back(to_string(num) + "/" + to_string(den));
}
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/08/01/PS/LeetCode/simplified-fractions/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.