Skip to content

Commit c7d4b49

Browse files
committed
263
1 parent ef51e7e commit c7d4b49

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,5 @@
225225
* [257. Binary Tree Paths](leetcode-257-Binary-Tree-Paths.md)
226226
* [258. Add Digits](leetcode-258-Add-Digits.md)
227227
* [260. Single Number III](leetcode-260-Single-NumberIII.md)
228+
* [263. Ugly Number](leetcode-263-Ugly-Number.md)
228229
* [更多](more.md)

leetcode-263-Ugly-Number.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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,很简单的一道题。

0 commit comments

Comments
 (0)