Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ab75341

Browse files
add power_of_four (#20)
1 parent 5eb96fc commit ab75341

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ cargo test
6464
0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/single-number-iii.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/single-number-iii.py) <br>------</br> [Rust](./kamyu104/src/single_number_iii.rs) | _O(n)_ | _O(1)_ | Medium ||
6565
0268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/missing-number.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/missing-number.py) <br>------</br> [Rust](./kamyu104/src/missing_number.rs) | _O(n)_ | _O(1)_ | Medium | LintCode ||
6666
0318| [Maximum Product of Word Lengths](https://leetcode.com/problems/maximum-product-of-word-lengths/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/maximum-product-of-word-lengths.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py) <br>------</br> [Rust](./kamyu104/src/maximum_product_of_word_lengths.rs) | _O(n)_ ~ _O(n^2)_ | _O(n)_ | Medium || Bit Manipulation, Counting Sort, Pruning|
67+
0342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](https://github.com/kamyu104/LeetCode-Solutions/blob/master/C++/power-of-four.cpp) [Python](https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/power-of-four.py) <br>------</br> [Rust](./kamyu104/src/power_of_four.rs) | _O(1)_ | _O(1)_ | Easy | |
6768

6869
<!--
6970
70-
0342 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](./C++/power-of-four.cpp) [Python](./Python/power-of-four.py) | _O(1)_ | _O(1)_ | Easy | |
7171
0371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode |
7272
0389 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [C++](./C++/find-the-difference.cpp) [Python](./Python/find-the-difference.py) | _O(n)_ | _O(1)_ | Easy | |
7373
0393 | [UTF-8 Validation](https://leetcode.com/problems/utf-8-validation/) | [C++](./C++/utf-8-validation.cpp) [Python](./Python/utf-8-validation.py) | _O(n)_ | _O(1)_ | Medium | |

kamyu104/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ mod bitwise_and_of_numbers_range;
22
mod maximum_product_of_word_lengths;
33
mod missing_number;
44
mod number_of_1_bits;
5+
mod power_of_four;
56
mod power_of_two;
67
mod reverse_bits;
78
mod single_number;

kamyu104/src/maximum_product_of_word_lengths.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ impl Solution2 {
4141
}
4242
}
4343

44-
4544
// # Time: O(n) ~ O(n^2)
4645
// # Space: O(n)
4746
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/maximum-product-of-word-lengths.py#L1-L36
@@ -97,4 +96,4 @@ mod tests {
9796
0
9897
);
9998
}
100-
}
99+
}

kamyu104/src/power_of_four.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
pub struct Solution1 {}
4+
impl Solution1 {
5+
pub fn is_power_of_four(num: i32) -> bool {
6+
num > 0 && (num & (num - 1)) == 0 && ((num & 0b01010101010101010101010101010101) == num)
7+
}
8+
}
9+
10+
// Time: O(1)
11+
// Space: O(1)
12+
pub struct Solution2 {}
13+
impl Solution2 {
14+
pub fn is_power_of_four(num: i32) -> bool {
15+
let mut n = num;
16+
while n != 0 && (n & 0b11) == 0 {
17+
n >>= 2;
18+
}
19+
n == 1
20+
}
21+
}
22+
23+
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/power-of-four.py#L27-L34
24+
// pub struct Solution3 {}
25+
// impl Solution3 {
26+
// pub fn is_power_of_four(num: i32) -> bool {
27+
// }
28+
// }
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
34+
#[test]
35+
fn test_is_power_of_four() {
36+
assert_eq!(Solution1::is_power_of_four(16), true);
37+
assert_eq!(Solution1::is_power_of_four(5), false);
38+
39+
assert_eq!(Solution2::is_power_of_four(16), true);
40+
assert_eq!(Solution2::is_power_of_four(5), false);
41+
42+
// assert_eq!(Solution3::is_power_of_four(16), true);
43+
// assert_eq!(Solution3::is_power_of_four(5), false);
44+
}
45+
}

0 commit comments

Comments
 (0)