[leetcode] 263. Ugly Number (easy)

it2025-11-13  5

只要存在一种因数分解后,其因子是2,3,5中的一种或多种,就算是ugly数字。

思路:

以2/3/5作为除数除后,最后结果等于1的就是ugly数字

Runtime: 4 ms, faster than 98.64% of C++ online submissions for Ugly Number.

class Solution { public: bool isUgly(int num) { while (num % 5 == 0 && num > 4) num /= 5; while (num % 3 == 0 && num > 2) num /= 3; while (num % 2 == 0 && num > 1) num /= 2; return num == 1; } };

 

转载于:https://www.cnblogs.com/ruoh3kou/p/10007074.html

相关资源:数据结构—成绩单生成器
最新回复(0)