-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path263.丑数.go
64 lines (63 loc) · 1.01 KB
/
263.丑数.go
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @lc app=leetcode.cn id=263 lang=golang
*
* [263] 丑数
*
* https://leetcode-cn.com/problems/ugly-number/description/
*
* algorithms
* Easy (46.39%)
* Likes: 54
* Dislikes: 0
* Total Accepted: 10K
* Total Submissions: 21.6K
* Testcase Example: '6'
*
* 编写一个程序判断给定的数是否为丑数。
*
* 丑数就是只包含质因数 2, 3, 5 的正整数。
*
* 示例 1:
*
* 输入: 6
* 输出: true
* 解释: 6 = 2 × 3
*
* 示例 2:
*
* 输入: 8
* 输出: true
* 解释: 8 = 2 × 2 × 2
*
*
* 示例 3:
*
* 输入: 14
* 输出: false
* 解释: 14 不是丑数,因为它包含了另外一个质因数 7。
*
* 说明:
*
*
* 1 是丑数。
* 输入不会超过 32 位有符号整数的范围: [−2^31, 2^31 − 1]。
*
*
*/
func isUgly(num int) bool {
if num <= 0 {
return false
}
for num > 1 {
if num%2 == 0 {
num /= 2
} else if num%3 == 0 {
num /= 3
} else if num%5 == 0 {
num /= 5
} else {
return false
}
}
return true
}