Skip to content

Commit b72043a

Browse files
committed
feat: update leetcode solutions: No.0263. Ugly Number
1 parent 381e8c1 commit b72043a

File tree

6 files changed

+196
-14
lines changed

6 files changed

+196
-14
lines changed

solution/0200-0299/0263.Ugly Number/README.md

+74-1
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,95 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42+
-`n < 1`,说明 n 一定不是丑数,返回 false。
43+
-`n % 2 == 0`,说明 2 是 n 的因子,此时应 `n /= 2`,然后继续判断 n 除以 2 后的值的因子。
44+
-`n % 3 == 0`,说明 3 是 n 的因子,此时应 `n /= 3`,然后继续判断 n 除以 3 后的值的因子。
45+
-`n % 5 == 0`,说明 5 是 n 的因子,此时应 `n /= 5`,然后继续判断 n 除以 5 后的值的因子。
46+
- 最后,判断 n 是否等于 1,若是,说明 n 的因子只可能包含 2、3、5,返回 true;否则返回 false。
47+
4248
<!-- tabs:start -->
4349

4450
### **Python3**
4551

4652
<!-- 这里可写当前语言的特殊实现逻辑 -->
4753

4854
```python
49-
55+
class Solution:
56+
def isUgly(self, n: int) -> bool:
57+
if n < 1:
58+
return False
59+
while n % 2 == 0:
60+
n //= 2
61+
while n % 3 == 0:
62+
n //= 3
63+
while n % 5 == 0:
64+
n //= 5
65+
return n == 1
5066
```
5167

5268
### **Java**
5369

5470
<!-- 这里可写当前语言的特殊实现逻辑 -->
5571

5672
```java
73+
class Solution {
74+
public boolean isUgly(int n) {
75+
if (n < 1) return false;
76+
while (n % 2 == 0) {
77+
n /= 2;
78+
}
79+
while (n % 3 == 0) {
80+
n /= 3;
81+
}
82+
while (n % 5 == 0) {
83+
n /= 5;
84+
}
85+
return n == 1;
86+
}
87+
}
88+
```
89+
90+
### **C++**
91+
92+
```cpp
93+
class Solution {
94+
public:
95+
bool isUgly(int n) {
96+
if (n < 1) return false;
97+
while (n % 2 == 0) {
98+
n /= 2;
99+
}
100+
while (n % 3 == 0) {
101+
n /= 3;
102+
}
103+
while (n % 5 == 0) {
104+
n /= 5;
105+
}
106+
return n == 1;
107+
}
108+
};
109+
```
57110
111+
### **JavaScript**
112+
113+
```js
114+
/**
115+
* @param {number} n
116+
* @return {boolean}
117+
*/
118+
var isUgly = function (n) {
119+
if (n < 1) return false;
120+
while (n % 2 == 0) {
121+
n /= 2;
122+
}
123+
while (n % 3 == 0) {
124+
n /= 3;
125+
}
126+
while (n % 5 == 0) {
127+
n /= 5;
128+
}
129+
return n == 1;
130+
};
58131
```
59132

60133
### **...**

solution/0200-0299/0263.Ugly Number/README_EN.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,80 @@
5656
### **Python3**
5757

5858
```python
59-
59+
class Solution:
60+
def isUgly(self, n: int) -> bool:
61+
if n < 1:
62+
return False
63+
while n % 2 == 0:
64+
n //= 2
65+
while n % 3 == 0:
66+
n //= 3
67+
while n % 5 == 0:
68+
n //= 5
69+
return n == 1
6070
```
6171

6272
### **Java**
6373

6474
```java
75+
class Solution {
76+
public boolean isUgly(int n) {
77+
if (n < 1) return false;
78+
while (n % 2 == 0) {
79+
n /= 2;
80+
}
81+
while (n % 3 == 0) {
82+
n /= 3;
83+
}
84+
while (n % 5 == 0) {
85+
n /= 5;
86+
}
87+
return n == 1;
88+
}
89+
}
90+
```
91+
92+
### **C++**
93+
94+
```cpp
95+
class Solution {
96+
public:
97+
bool isUgly(int n) {
98+
if (n < 1) return false;
99+
while (n % 2 == 0) {
100+
n /= 2;
101+
}
102+
while (n % 3 == 0) {
103+
n /= 3;
104+
}
105+
while (n % 5 == 0) {
106+
n /= 5;
107+
}
108+
return n == 1;
109+
}
110+
};
111+
```
65112
113+
### **JavaScript**
114+
115+
```js
116+
/**
117+
* @param {number} n
118+
* @return {boolean}
119+
*/
120+
var isUgly = function (n) {
121+
if (n < 1) return false;
122+
while (n % 2 == 0) {
123+
n /= 2;
124+
}
125+
while (n % 3 == 0) {
126+
n /= 3;
127+
}
128+
while (n % 5 == 0) {
129+
n /= 5;
130+
}
131+
return n == 1;
132+
};
66133
```
67134

68135
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
bool isUgly(int n) {
4+
if (n < 1) return false;
5+
while (n % 2 == 0) {
6+
n /= 2;
7+
}
8+
while (n % 3 == 0) {
9+
n /= 3;
10+
}
11+
while (n % 5 == 0) {
12+
n /= 5;
13+
}
14+
return n == 1;
15+
}
16+
};
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
class Solution {
2-
public boolean isUgly(int num) {
3-
if (num < 1) {
4-
return false;
2+
public boolean isUgly(int n) {
3+
if (n < 1) return false;
4+
while (n % 2 == 0) {
5+
n /= 2;
56
}
6-
while (num % 2 == 0) {
7-
num /= 2;
7+
while (n % 3 == 0) {
8+
n /= 3;
89
}
9-
while (num % 3 == 0) {
10-
num /= 3;
10+
while (n % 5 == 0) {
11+
n /= 5;
1112
}
12-
while (num % 5 == 0) {
13-
num /= 5;
14-
}
15-
return num == 1;
13+
return n == 1;
1614
}
17-
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @return {boolean}
4+
*/
5+
var isUgly = function (n) {
6+
if (n < 1) return false;
7+
while (n % 2 == 0) {
8+
n /= 2;
9+
}
10+
while (n % 3 == 0) {
11+
n /= 3;
12+
}
13+
while (n % 5 == 0) {
14+
n /= 5;
15+
}
16+
return n == 1;
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isUgly(self, n: int) -> bool:
3+
if n < 1:
4+
return False
5+
while n % 2 == 0:
6+
n //= 2
7+
while n % 3 == 0:
8+
n //= 3
9+
while n % 5 == 0:
10+
n //= 5
11+
return n == 1

0 commit comments

Comments
 (0)