1 class Solution: 2 def closestDivisors(self, num: int) -> List[int]: 3 d = float("inf") 4 res = [] 5 for i in range(1, int((num + 1) ** 0.5 + 1)): 6 if (num + 1) % i == 0: 7 if (num + 1) // i - i < d: 8 d = (num + 1) // i - i 9 res = [i, (num + 1) // i] 10 for i in range(1, int((num + 2) ** 0.5 + 1)): 11 if (num + 2) % i == 0: 12 if (num + 2) // i - i < d: 13 d = (num + 2) // i - i 14 res = [i, (num + 2) // i] 15 return res
参考:https://leetcode.com/problems/closest-divisors/discuss/518058/python-3-easy-to-understand