-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy path0326_power_of_three.rs
More file actions
103 lines (92 loc) 路 2.02 KB
/
Copy path0326_power_of_three.rs
File metadata and controls
103 lines (92 loc) 路 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Given an integer, write a function to determine if it is a power of three.
//!
//! ```
//! Example 1:
//!
//! Input: 27
//! Output: true
//! ```
//!
//! ```
//! Example 2:
//!
//! Input: 0
//! Output: false
//! ```
//!
//! ```
//! Example 3:
//!
//! Input: 9
//! Output: true
//! ```
//!
//! ```
//! Example 4:
//!
//! Input: 45
//! Output: false
//! ```
//!
//! Follow up:
//! Could you do it without using any loop / recursion?
use std::collections::HashSet;
struct Solution {}
impl Solution {
pub fn is_power_of_three(n: i32) -> bool {
match n {
1 => {
true
}
2 => {
false
}
_ => {
let mut r: i64 = 3;
while r < (n as i64) {
r *= 3;
}
r == (n as i64)
}
}
}
pub fn is_power_of_three2(n: i32) -> bool {
let mut set: HashSet<i32> = [
1, 3, 9, 27,
81, 243, 729, 2187,
6561, 19683, 59049, 177147,
531441, 1594323, 4782969, 14348907,
43046721, 129140163, 387420489, 1162261467
].iter().cloned().collect();
set.contains(&n)
}
}
#[cfg(test)]
mod tests {
use super::Solution;
#[test]
fn test_0() {
assert_eq!(Solution::is_power_of_three(27), true);
assert_eq!(Solution::is_power_of_three2(27), true);
}
#[test]
fn test_1() {
assert_eq!(Solution::is_power_of_three(0), false);
assert_eq!(Solution::is_power_of_three2(0), false);
}
#[test]
fn test_2() {
assert_eq!(Solution::is_power_of_three(9), true);
assert_eq!(Solution::is_power_of_three2(9), true);
}
#[test]
fn test_3() {
assert_eq!(Solution::is_power_of_three(45), false);
assert_eq!(Solution::is_power_of_three2(45), false);
}
#[test]
fn test_4() {
assert_eq!(Solution::is_power_of_three(1), true);
assert_eq!(Solution::is_power_of_three2(1), true);
}
}