Leetcode 263. Ugly Number

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Ugly Number

2. Solution

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
class Solution {
public:
bool isUgly(int num) {
if(num <= 0) {
return false;
}
if(num == 1) {
return true;
}
while(num != 1) {
if(num % 2 == 0) {
num = num / 2;
}
else if(num % 3 == 0) {
num = num / 3;
}
else if(num % 5 == 0) {
num = num / 5;
}
else {
return false;
}
}
return true;
}
};

Reference

  1. https://leetcode.com/problems/ugly-number/description/
如果有收获,可以请我喝杯咖啡!