Skip to content

Commit 8ccdd82

Browse files
committed
feat: add solutions to lc problem: No.1447
No.1447.Simplified Fractions
1 parent 45410c9 commit 8ccdd82

File tree

6 files changed

+165
-2
lines changed

6 files changed

+165
-2
lines changed

solution/1400-1499/1447.Simplified Fractions/README.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,72 @@
5353
<!-- 这里可写当前语言的特殊实现逻辑 -->
5454

5555
```python
56-
56+
class Solution:
57+
def simplifiedFractions(self, n: int) -> List[str]:
58+
return [f'{i}/{j}' for i in range(1, n) for j in range(i + 1, n + 1) if gcd(i, j) == 1]
5759
```
5860

5961
### **Java**
6062

6163
<!-- 这里可写当前语言的特殊实现逻辑 -->
6264

6365
```java
66+
class Solution {
67+
public List<String> simplifiedFractions(int n) {
68+
List<String> ans = new ArrayList<>();
69+
for (int i = 1; i < n; ++i) {
70+
for (int j = i + 1; j < n + 1; ++j) {
71+
if (gcd(i, j) == 1) {
72+
ans.add(i + "/" + j);
73+
}
74+
}
75+
}
76+
return ans;
77+
}
78+
79+
private int gcd(int a, int b) {
80+
return b > 0 ? gcd(b, a % b) : a;
81+
}
82+
}
83+
```
84+
85+
### **C++**
86+
87+
```cpp
88+
class Solution {
89+
public:
90+
vector<string> simplifiedFractions(int n) {
91+
vector<string> ans;
92+
for (int i = 1; i < n; ++i)
93+
for (int j = i + 1; j < n + 1; ++j)
94+
if (gcd(i, j) == 1)
95+
ans.push_back(to_string(i) + "/" + to_string(j));
96+
return ans;
97+
}
98+
};
99+
```
64100
101+
### **Go**
102+
103+
```go
104+
func simplifiedFractions(n int) []string {
105+
var ans []string
106+
for i := 1; i < n; i++ {
107+
for j := i + 1; j < n+1; j++ {
108+
if gcd(i, j) == 1 {
109+
ans = append(ans, strconv.Itoa(i)+"/"+strconv.Itoa(j))
110+
}
111+
}
112+
}
113+
return ans
114+
}
115+
116+
func gcd(a, b int) int {
117+
if b <= 0 {
118+
return a
119+
}
120+
return gcd(b, a%b)
121+
}
65122
```
66123

67124
### **...**

solution/1400-1499/1447.Simplified Fractions/README_EN.md

+58-1
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,70 @@
6363
### **Python3**
6464

6565
```python
66-
66+
class Solution:
67+
def simplifiedFractions(self, n: int) -> List[str]:
68+
return [f'{i}/{j}' for i in range(1, n) for j in range(i + 1, n + 1) if gcd(i, j) == 1]
6769
```
6870

6971
### **Java**
7072

7173
```java
74+
class Solution {
75+
public List<String> simplifiedFractions(int n) {
76+
List<String> ans = new ArrayList<>();
77+
for (int i = 1; i < n; ++i) {
78+
for (int j = i + 1; j < n + 1; ++j) {
79+
if (gcd(i, j) == 1) {
80+
ans.add(i + "/" + j);
81+
}
82+
}
83+
}
84+
return ans;
85+
}
86+
87+
private int gcd(int a, int b) {
88+
return b > 0 ? gcd(b, a % b) : a;
89+
}
90+
}
91+
```
92+
93+
### **C++**
94+
95+
```cpp
96+
class Solution {
97+
public:
98+
vector<string> simplifiedFractions(int n) {
99+
vector<string> ans;
100+
for (int i = 1; i < n; ++i)
101+
for (int j = i + 1; j < n + 1; ++j)
102+
if (gcd(i, j) == 1)
103+
ans.push_back(to_string(i) + "/" + to_string(j));
104+
return ans;
105+
}
106+
};
107+
```
72108
109+
### **Go**
110+
111+
```go
112+
func simplifiedFractions(n int) []string {
113+
var ans []string
114+
for i := 1; i < n; i++ {
115+
for j := i + 1; j < n+1; j++ {
116+
if gcd(i, j) == 1 {
117+
ans = append(ans, strconv.Itoa(i)+"/"+strconv.Itoa(j))
118+
}
119+
}
120+
}
121+
return ans
122+
}
123+
124+
func gcd(a, b int) int {
125+
if b <= 0 {
126+
return a
127+
}
128+
return gcd(b, a%b)
129+
}
73130
```
74131

75132
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
public:
3+
vector<string> simplifiedFractions(int n) {
4+
vector<string> ans;
5+
for (int i = 1; i < n; ++i)
6+
for (int j = i + 1; j < n + 1; ++j)
7+
if (gcd(i, j) == 1)
8+
ans.push_back(to_string(i) + "/" + to_string(j));
9+
return ans;
10+
}
11+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func simplifiedFractions(n int) []string {
2+
var ans []string
3+
for i := 1; i < n; i++ {
4+
for j := i + 1; j < n+1; j++ {
5+
if gcd(i, j) == 1 {
6+
ans = append(ans, strconv.Itoa(i)+"/"+strconv.Itoa(j))
7+
}
8+
}
9+
}
10+
return ans
11+
}
12+
13+
func gcd(a, b int) int {
14+
if b <= 0 {
15+
return a
16+
}
17+
return gcd(b, a%b)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<String> simplifiedFractions(int n) {
3+
List<String> ans = new ArrayList<>();
4+
for (int i = 1; i < n; ++i) {
5+
for (int j = i + 1; j < n + 1; ++j) {
6+
if (gcd(i, j) == 1) {
7+
ans.add(i + "/" + j);
8+
}
9+
}
10+
}
11+
return ans;
12+
}
13+
14+
private int gcd(int a, int b) {
15+
return b > 0 ? gcd(b, a % b) : a;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def simplifiedFractions(self, n: int) -> List[str]:
3+
return [f'{i}/{j}' for i in range(1, n) for j in range(i + 1, n + 1) if gcd(i, j) == 1]

0 commit comments

Comments
 (0)