当前位置:网站首页>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)
边栏推荐
- Soft music -js find the number of times that character appears in the string - Feng Hao's blog
- Discussion on QWidget code setting style sheet
- CMake Error: Could not create named generator Visual Studio 16 2019解决方法
- 本地可视化工具连接阿里云centOS服务器的redis
- Date plus 1 day
- Research Report on market supply and demand and strategy of China's four flat leadless (QFN) packaging industry
- Chapter 5 namenode and secondarynamenode
- Chapter 6 rebalance details
- Investigation report of bench type Brinell hardness tester industry - market status analysis and development prospect prediction
- 生成随机密码/验证码
猜你喜欢
Codeforces Round #802(Div. 2)A~D
<li>圆点样式 list-style-type
It is forbidden to trigger onchange in antd upload beforeupload
Submit several problem records of spark application (sparklauncher with cluster deploy mode)
Business system compatible database oracle/postgresql (opengauss) /mysql Trivia
本地可视化工具连接阿里云centOS服务器的redis
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
原生js实现全选和反选的功能 --冯浩的博客
Anaconda下安装Jupyter notebook
业务系统从Oracle迁移到openGauss数据库的简单记录
随机推荐
Chapter 1 overview of MapReduce
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
js封装数组反转的方法--冯浩的博客
Summary of FTP function implemented by qnetworkaccessmanager
Acwing: Game 58 of the week
MariaDB的安装与配置
China double brightening film (dbef) market trend report, technical dynamic innovation and market forecast
简单尝试DeepFaceLab(DeepFake)的新AMP模型
OneForAll安装使用
Pull branch failed, fatal: 'origin/xxx' is not a commit and a branch 'xxx' cannot be created from it
字节跳动新程序员成长秘诀:那些闪闪发光的宝藏mentor们
Market trend report, technical innovation and market forecast of double-sided foam tape in China
视频压缩编码和音频压缩编码基本原理
软通乐学-js求字符串中字符串当中那个字符出现的次数多 -冯浩的博客
Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
Problem - 1646C. Factorials and Powers of Two - Codeforces
Codeforces Round #801 (Div. 2)A~C
Gridhome, a static site generator that novices must know
Bidirectional linked list - all operations
input 只能输入数字,限定输入