57
58
kamyu104/src/bitwise_and_of_numbers_range.rsCopy file name to clipboard Expand all lines: kamyu104/src/bitwise_and_of_numbers_range.rs +1 -2 Lines changed: 1 addition & 2 deletions Original file line number Diff line number Diff line change 2
2
// Space: O(1)
3
3
4
4
pub struct Solution1 { }
5
- pub struct Solution2 { }
6
-
7
5
impl Solution1 {
8
6
pub fn range_bitwise_and ( m : u32 , mut n : u32 ) -> u32 {
9
7
while m < n {
@@ -14,6 +12,7 @@ impl Solution1 {
14
12
}
15
13
}
16
14
15
+ pub struct Solution2 { }
17
16
impl Solution2 {
18
17
pub fn range_bitwise_and ( m : u32 , n : u32 ) -> u32 {
19
18
let ( mut i, mut diff) : ( u32 , u32 ) = ( 0 , n - m) ;
Original file line number Diff line number Diff line change 1
1
mod bitwise_and_of_numbers_range;
2
2
mod number_of_1_bits;
3
+ mod power_of_two;
3
4
mod reverse_bits;
4
5
mod single_number;
5
6
mod single_number_ii;
Original file line number Diff line number Diff line change 2
2
// Space: O(1)
3
3
4
4
pub struct Solution { }
5
-
6
5
impl Solution {
7
6
pub fn hamming_weight ( mut n : u32 ) -> u8 {
8
7
let mut cnt: u8 = 0 ;
Original file line number Diff line number Diff line change
1
+ // Time: O(1)
2
+ // Space: O(1)
3
+
4
+ pub struct Solution1 { }
5
+ impl Solution1 {
6
+ pub fn is_power_of_two ( n : i32 ) -> bool {
7
+ n > 0 && ( n & ( n - 1 ) ) == 0
8
+ }
9
+ }
10
+
11
+ pub struct Solution2 { }
12
+ impl Solution2 {
13
+ pub fn is_power_of_two ( n : i32 ) -> bool {
14
+ n > 0 && ( n & !-n) == 0
15
+ }
16
+ }
17
+
18
+
19
+ #[ cfg( test) ]
20
+ mod tests {
21
+ // Note this useful idiom: importing names from outer (for mod tests) scope.
22
+ use super :: * ;
23
+
24
+ #[ test]
25
+ fn test_is_power_of_two ( ) {
26
+ assert_eq ! ( Solution1 :: is_power_of_two( 1 ) , true ) ;
27
+ assert_eq ! ( Solution1 :: is_power_of_two( 16 ) , true ) ;
28
+ assert_eq ! ( Solution1 :: is_power_of_two( 218 ) , false ) ;
29
+
30
+ assert_eq ! ( Solution2 :: is_power_of_two( 1 ) , true ) ;
31
+ assert_eq ! ( Solution2 :: is_power_of_two( 16 ) , true ) ;
32
+ assert_eq ! ( Solution2 :: is_power_of_two( 218 ) , false ) ;
33
+ }
34
+ }
35
+
Original file line number Diff line number Diff line change 2
2
// Space: O(1)
3
3
4
4
pub struct Solution { }
5
-
6
5
impl Solution {
7
6
pub fn reverse_bits ( mut n : u32 ) -> u32 {
8
7
let mut result: u32 = 0 ;
Original file line number Diff line number Diff line change 2
2
// Space: O(1)
3
3
4
4
pub struct Solution { }
5
-
6
5
impl Solution {
7
6
pub fn single_number ( nums : Vec < i32 > ) -> i32 {
8
7
nums. iter ( ) . fold ( 0 , |acc, & num| acc ^ num)
Original file line number Diff line number Diff line change 4
4
use std:: collections:: HashSet ;
5
5
6
6
pub struct Solution1 { }
7
- pub struct Solution2 { }
8
- pub struct Solution3 { }
9
- pub struct Solution4 { }
10
-
11
7
impl Solution1 {
12
8
pub fn single_number ( nums : Vec < i32 > ) -> i32 {
13
9
let ( mut one, mut two) : ( i32 , i32 ) = ( 0 , 0 ) ;
@@ -21,6 +17,7 @@ impl Solution1 {
21
17
}
22
18
}
23
19
20
+ pub struct Solution2 { }
24
21
impl Solution2 {
25
22
pub fn single_number ( nums : Vec < i32 > ) -> i32 {
26
23
let ( mut one, mut two, mut carry) : ( i32 , i32 , i32 ) = ( 0 , 0 , 0 ) ;
@@ -36,11 +33,13 @@ impl Solution2 {
36
33
}
37
34
38
35
// https://github.com/kamyu104/LeetCode-Solutions/blob/master/Python/single-number-ii.py#L31
36
+ // pub struct Solution3 {}
39
37
// impl Solution3 {
40
38
// pub fn single_number(nums: Vec) -> i32 {
41
39
// }
42
40
// }
43
41
42
+ pub struct Solution4 { }
44
43
impl Solution4 {
45
44
pub fn single_number ( nums : Vec < i32 > ) -> i32 {
46
45
let sum: i32 = nums. iter ( ) . sum ( ) ;
Original file line number Diff line number Diff line change 1
1
pub struct Solution { }
2
-
3
2
impl Solution {
4
3
pub fn add ( a : i32 , b : i32 ) -> i32 {
5
4
// only print when a test fails
You can’t perform that action at this time.
0 commit comments