File tree Expand file tree Collapse file tree 3 files changed +44
-0
lines changed
1447-Simplified-Fractions/cpp-1447 Expand file tree Collapse file tree 3 files changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.16 )
2
+ project (cpp_1447 )
3
+
4
+ set (CMAKE_CXX_STANDARD 14 )
5
+
6
+ add_executable (cpp_1447 main.cpp )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/simplified-fractions/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2020-05-16
4
+
5
+ #include < iostream>
6
+ #include < vector>
7
+ #include < unordered_set>
8
+
9
+ using namespace std ;
10
+
11
+
12
+ // / Brute Force + GCD + HashSet
13
+ // / Time Complexity: O(n^2)
14
+ // / Space Complexity: O(n^2)
15
+ class Solution {
16
+ public:
17
+ vector<string> simplifiedFractions (int n) {
18
+
19
+ unordered_set<string> res;
20
+ for (int d = 2 ; d <= n; d ++)
21
+ for (int i = 1 ; i < d; i ++){
22
+ int g = gcd (d, i);
23
+ res.insert (to_string (i / g) + " /" + to_string (d / g));
24
+ }
25
+ return vector<string>(res.begin (), res.end ());
26
+ }
27
+
28
+ private:
29
+ int gcd (int a, int b){
30
+ return b == 0 ? a : gcd (b, a % b);
31
+ }
32
+ };
33
+
34
+ int main () {
35
+
36
+ return 0 ;
37
+ }
Original file line number Diff line number Diff line change @@ -1047,6 +1047,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
1047
1047
| 1444 | [ Number of Ways of Cutting a Pizza] ( https://leetcode.com/problems/number-of-ways-of-cutting-a-pizza/ ) | [ 无] | [ C++] ( 1444-Number-of-Ways-of-Cutting-a-Pizza/cpp-1444/ ) | | |
1048
1048
| | | | | | |
1049
1049
| 1446 | [ Consecutive Characters] ( https://leetcode.com/problems/consecutive-characters/ ) | [ 无] | [ C++] ( 1446-Consecutive-Characters/cpp-1446/ ) | | |
1050
+ | 1447 | [ Simplified Fractions] ( https://leetcode.com/problems/simplified-fractions/ ) | [ 无] | [ C++] ( 1447-Simplified-Fractions/cpp-1447/ ) | | |
1050
1051
| | | | | | |
1051
1052
1052
1053
## 力扣中文站比赛
You can’t perform that action at this time.
0 commit comments