File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -208,3 +208,4 @@ mod n0242_valid_anagram;
208
208
mod n0257_binary_tree_paths;
209
209
mod n0258_add_digits;
210
210
mod n0260_single_number_iii;
211
+ mod n0263_ugly_number;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments