Skip to content

Commit 401e865

Browse files
committed
"Ugly Number"
1 parent 7ea5439 commit 401e865

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2727
| --- | ------------------------------------------------------------ | ------------------ |
2828
| 265 | [Paint House II] | |
2929
| 264 | [Ugly Number II] | |
30-
| 263 | [Ugly Number] | |
30+
| 263 | [Ugly Number] | [C](src/263.c) |
3131
| 261 | [Graph Valid Tree]| |
3232
| 260 | [Single Number III] | |
3333
| 259 | [3Sum Smaller]| |

src/263.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
#include <assert.h>
4+
5+
bool isUgly(int num) {
6+
if (num <= 0) return false;
7+
8+
while (num > 1) {
9+
bool flag = false;
10+
11+
if (num % 2 == 0) {
12+
flag = true;
13+
num /= 2;
14+
}
15+
if (num % 3 == 0) {
16+
flag = true;
17+
num /= 3;
18+
}
19+
if (num % 5 == 0) {
20+
flag = true;
21+
num /= 5;
22+
}
23+
24+
if (!flag) return false;
25+
}
26+
27+
return true;
28+
}
29+
30+
int main() {
31+
assert(isUgly(0) == false);
32+
assert(isUgly(1) == true);
33+
assert(isUgly(6) == true);
34+
assert(isUgly(14) == false);
35+
36+
printf("all test passed.\n");
37+
return 0;
38+
}

0 commit comments

Comments
 (0)