LeetCode Leetcode 1447 Simplified Fractions (Python)

Posted by 小明MaxMing on May 16, 2020

题目

Given an integer n, return a list of all simplified fractions between 0 and 1 (exclusive) such that the denominator is less-than-or-equal-to n. The fractions can be in any order.

解题思路

遍历所有分子分母的可能,如果分子分母的最大公约数是1,则是一个要返回的结果

代码

class Solution:
    def simplifiedFractions(self, n: int) -> List[str]:
        def gcd(x, y):
            return y if x == 0 else gcd(y % x, x)
        
        return ["%s/%s" % (i, j) for j in range(2, n + 1) for i in range(1, j) if gcd(i, j) == 1]

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅