File tree Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Expand file tree Collapse file tree 2 files changed +47
-0
lines changed Original file line number Diff line number Diff line change 225
225
* [ 257. Binary Tree Paths] ( leetcode-257-Binary-Tree-Paths.md )
226
226
* [ 258. Add Digits] ( leetcode-258-Add-Digits.md )
227
227
* [ 260. Single Number III] ( leetcode-260-Single-NumberIII.md )
228
+ * [ 263. Ugly Number] ( leetcode-263-Ugly-Number.md )
228
229
* [ 更多] ( more.md )
Original file line number Diff line number Diff line change
1
+ # 题目描述(简单难度)
2
+
3
+ ![ ] ( https://windliang.oss-cn-beijing.aliyuncs.com/263.png )
4
+
5
+ 判断是否是丑数,丑数的质数因子中仅含有 ` 2, 3, 5 ` 。
6
+
7
+ # 解法一
8
+
9
+ 可以用递归的思想去写,判断能否被 ` 2, 3, 5 ` 整除,如果能整除的话,就去递归。
10
+
11
+ ``` java
12
+ public boolean isUgly(int num) {
13
+ if (num <= 0 ) {
14
+ return false ;
15
+ }
16
+ if (num % 2 == 0 ) {
17
+ return isUgly(num / 2 );
18
+ }
19
+
20
+ if (num % 3 == 0 ) {
21
+ return isUgly(num / 3 );
22
+ }
23
+
24
+ if (num % 5 == 0 ) {
25
+ return isUgly(num / 5 );
26
+ }
27
+
28
+ return num == 1 ;
29
+ }
30
+ ```
31
+
32
+ 还可以直接用 ` while ` 循环,分享 [ 这里] ( https://leetcode.com/problems/ugly-number/discuss/69342/Simplest-java-solution ) 的解法。
33
+
34
+ ``` java
35
+ public boolean isUgly(int num) {
36
+ if (num <= 0 ) return false ;
37
+ while (num % 2 == 0 ) num /= 2 ;
38
+ while (num % 3 == 0 ) num /= 3 ;
39
+ while (num % 5 == 0 ) num /= 5 ;
40
+ return num == 1 ;
41
+ }
42
+ ```
43
+
44
+ # 总
45
+
46
+ emm,很简单的一道题。
You can’t perform that action at this time.
0 commit comments