Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6a8088b

Browse files
committed
Solve #263
1 parent 2369db6 commit 6a8088b

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,4 @@ mod n0242_valid_anagram;
208208
mod n0257_binary_tree_paths;
209209
mod n0258_add_digits;
210210
mod n0260_single_number_iii;
211+
mod n0263_ugly_number;

‎src/n0263_ugly_number.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* [263] Ugly Number
3+
*
4+
* Write a program to check whether a given number is an ugly number.
5+
*
6+
* Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: 6
12+
* Output: true
13+
* Explanation: 6 = 2 × 3
14+
*
15+
* Example 2:
16+
*
17+
*
18+
* Input: 8
19+
* Output: true
20+
* Explanation: 8 = 2 × 2 × 2
21+
*
22+
*
23+
* Example 3:
24+
*
25+
*
26+
* Input: 14
27+
* Output: false
28+
* Explanation: 14 is not ugly since it includes another prime factor 7.
29+
*
30+
*
31+
* Note:
32+
*
33+
* <ol>
34+
* 1 is typically treated as an ugly number.
35+
* Input is within the 32-bit signed integer range: [-2^31, 2^31 - 1].
36+
* </ol>
37+
*/
38+
pub struct Solution {}
39+
40+
// submission codes start here
41+
42+
impl Solution {
43+
pub fn is_ugly(num: i32) -> bool {
44+
if num <= 0 {
45+
false
46+
} else if num == 1 {
47+
true
48+
} else if num % 5 == 0 {
49+
Solution::is_ugly(num/5)
50+
} else if num % 3 == 0 {
51+
Solution::is_ugly(num/3)
52+
} else if num % 2 == 0 {
53+
Solution::is_ugly(num/2)
54+
} else {
55+
false
56+
}
57+
}
58+
}
59+
60+
// submission codes end
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_263() {
68+
assert_eq!(Solution::is_ugly(25), true);
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /