当前位置:网站首页>LeetCode 1447. Simplest fraction
LeetCode 1447. Simplest fraction
2022-07-06 16:42:00 【Daylight629】
1447. Simplest fraction
Give you an integer n
, Please return to all 0 To 1 Between ( barring 0 and 1) Satisfy that the denominator is less than or equal to n
Of Simplest fraction . The score can be in the form of arbitrarily Sequential return .
Example 1:
Input :n = 2
Output :["1/2"]
explain :"1/2" Is the only denominator less than or equal to 2 The simplest fraction of .
Example 2:
Input :n = 3
Output :["1/2","1/3","2/3"]
Example 3:
Input :n = 4
Output :["1/2","1/3","1/4","2/3","3/4"]
explain :"2/4" It's not the simplest fraction , Because it can be reduced to "1/2" .
Example 4:
Input :n = 1
Output :[]
Tips :
1 <= n <= 100
Two 、 Method 1
class Solution {
public List<String> simplifiedFractions(int n) {
List<String> res = new ArrayList<>();
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if(gcb(i, j) == 1) res.add(i + "/" + j);
}
}
return res;
}
// More derogation
public int gcb(int a, int b) {
while(true) {
if (a > b) a -= b;
else if (b > a) b -= a;
else return a;
}
}
}
Complexity analysis
- Time complexity : The complexity of enumerating numerators and denominators is O(n^2); The complexity of judging whether two numbers can be combined into the simplest fraction is O(logn). The overall complexity is O(n2∗logn)
- Spatial complexity : Ignore the extra space overhead caused by recursion , The complexity is O(1)
3、 ... and 、 Method 2
class Solution {
public List<String> simplifiedFractions(int n) {
List<String> res = new ArrayList<>();
for (int i = 1; i < n; i++) {
for (int j = i + 1; j <= n; j++) {
if(gcb(i, j) == 1) res.add(i + "/" + j);
}
}
return res;
}
// Euclid algorithm
public int gcb(int a, int b) {
return b == 0 ? a : gcb(b, a % b);
}
}
Complexity analysis
- Time complexity : The complexity of enumerating numerators and denominators is O(n^2); The complexity of judging whether two numbers can be combined into the simplest fraction is O(logn). The overall complexity is O(n2∗logn)
- Spatial complexity : Ignore the extra space overhead caused by recursion , The complexity is O(1)
边栏推荐
- Raspberry pie 4b64 bit system installation miniconda (it took a few days to finally solve it)
- 顺丰科技智慧物流校园技术挑战赛(无t4)
- Chapter 5 namenode and secondarynamenode
- (lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
- SQL quick start
- Chapter 7__ consumer_ offsets topic
- useEffect,函数组件挂载和卸载时触发
- Bidirectional linked list - all operations
- Cmake Express
- 我在字节跳动「修电影」
猜你喜欢
图像处理一百题(11-20)
第一章 MapReduce概述
Chapter 6 rebalance details
Local visualization tools are connected to redis of Alibaba cloud CentOS server
MP4格式详解
第6章 Rebalance详解
Click QT button to switch qlineedit focus (including code)
Codeforces round 797 (Div. 3) no f
Chapter 5 namenode and secondarynamenode
Codeforces Round #799 (Div. 4)A~H
随机推荐
Kubernetes集群部署
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
Chapter III principles of MapReduce framework
Simple records of business system migration from Oracle to opengauss database
Spark's RDD (elastic distributed data set) returns a large result set
第5章 消费者组详解
第一章 MapReduce概述
js封装数组反转的方法--冯浩的博客
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
It is forbidden to trigger onchange in antd upload beforeupload
Educational Codeforces Round 130 (Rated for Div. 2)A~C
Codeforces Round #800 (Div. 2)AC
300th weekly match - leetcode
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
Audio and video development interview questions
Market trend report, technological innovation and market forecast of double door and multi door refrigerators in China
875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
Soft music -js find the number of times that character appears in the string - Feng Hao's blog
去掉input聚焦时的边框
Market trend report, technological innovation and market forecast of desktop electric tools in China