題目:原題鏈接(簡單)
解法 | 時間複雜度 | 空間複雜度 | 執行用時 |
---|---|---|---|
Ans 1 (Python) | O(logn) | O(1) | 48ms (43.23%) |
LeetCode的Python執行用時隨緣,只要時間複雜度沒有明顯差異,執行用時一般都在同一個量級,僅作參考意義。
解法一(迭代除質因子,若醜數最後一定剩餘1):
def isUgly(self, num: int) -> bool:
if num <=0:
return False
while num > 1:
if num % 2 == 0:
num /= 2
elif num % 3 == 0:
num /= 3
elif num % 5 == 0:
num /= 5
else:
return False
else:
return True