leetcode-26双周赛-5397-最简分数

题目描述:

 

 提交:

class Solution:
    def simplifiedFractions(self, n: int) -> List[str]:
        
        def gcd(a, b):
            if b > a:
                return gcd(b, a)

            if a % b == 0:
                return b

            return gcd(b, a % b)
        
        res = set()
        if n == 1:
            return []
        for i in range(2,n+1):
            for j in range(1,i):
                if gcd(j,i) == 1:
                    res.add(str(j)+"/"+str(i))
        return list(res)

猜你喜欢

转载自www.cnblogs.com/oldby/p/12904736.html