1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| struct Solution;
// @lc code=start
impl Solution {
/// ## 解题思路
/// - 递归
pub fn is_ugly0(n: i32) -> bool {
match n {
0 => false,
1 | 2 | 3 | 5 => true,
_ => {
(n % 2 == 0 && Solution::is_ugly(n / 2))
|| (n % 3 == 0 && Solution::is_ugly(n / 3))
|| (n % 5 == 0 && Solution::is_ugly(n / 5))
}
}
}
/// - 迭代
pub fn is_ugly(n: i32) -> bool {
match n {
0 => false,
1 | 2 | 3 | 5 => true,
mut n => {
for p in [2, 3, 5] {
while n % p == 0 {
n /= p;
}
}
n == 1
}
}
}
}
// @lc code=end
|