当前位置:网站首页>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)
边栏推荐
- MariaDB的安装与配置
- 本地可视化工具连接阿里云centOS服务器的redis
- 300th weekly match - leetcode
- Research Report on market supply and demand and strategy of Chinese table lamp industry
- 第5章 消费者组详解
- Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
- ByteDance new programmer's growth secret: those glittering treasures mentors
- Basic principles of video compression coding and audio compression coding
- CMake速成
- Spark独立集群Worker和Executor的概念
猜你喜欢
Audio and video development interview questions
原生js实现全选和反选的功能 --冯浩的博客
Sublime text code formatting operation
Kubernetes cluster deployment
Spark independent cluster dynamic online and offline worker node
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
antd upload beforeUpload中禁止触发onchange
Li Kou - 298th weekly match
QT realizes window topping, topping state switching, and multi window topping priority relationship
Base dice (dynamic programming + matrix fast power)
随机推荐
Acwing: Game 58 of the week
CMake速成
Sublime text code formatting operation
Chapter 7__ consumer_ offsets topic
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
<li>圆点样式 list-style-type
SF smart logistics Campus Technology Challenge (no T4)
Anaconda下安装Jupyter notebook
JS time function Daquan detailed explanation ----- AHAO blog
Spark独立集群动态上线下线Worker节点
Li Kou: the 81st biweekly match
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Market trend report, technological innovation and market forecast of China's double sided flexible printed circuit board (FPC)
300th weekly match - leetcode
Chapter 5 detailed explanation of consumer groups
Codeforces - 1526C1&&C2 - Potions
OneForAll安装使用
The concept of spark independent cluster worker and executor
Gridhome, a static site generator that novices must know
Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction