58
59
kamyu104/src/main.rsCopy file name to clipboard Expand all lines: kamyu104/src/main.rs +1 Lines changed: 1 addition & 0 deletions Original file line number Diff line number Diff line change @@ -4,4 +4,5 @@ mod power_of_two;
4
4
mod reverse_bits;
5
5
mod single_number;
6
6
mod single_number_ii;
7
+ mod single_number_iii;
7
8
mod template;
Original file line number Diff line number Diff line change @@ -15,10 +15,8 @@ impl Solution2 {
15
15
}
16
16
}
17
17
18
-
19
18
#[ cfg( test) ]
20
19
mod tests {
21
- // Note this useful idiom: importing names from outer (for mod tests) scope.
22
20
use super :: * ;
23
21
24
22
#[ test]
@@ -32,4 +30,3 @@ mod tests {
32
30
assert_eq ! ( Solution2 :: is_power_of_two( 218 ) , false ) ;
33
31
}
34
32
}
35
-
Original file line number Diff line number Diff line change 1
1
// Time : O(logn) = O(32)
2
2
// Space: O(1)
3
3
4
- pub struct Solution { }
5
- impl Solution {
4
+ pub struct Solution1 { }
5
+ impl Solution1 {
6
6
pub fn reverse_bits ( mut n : u32 ) -> u32 {
7
7
let mut result: u32 = 0 ;
8
- for _ in ( 0 ..32 ) {
8
+ ( 0 ..32 ) . for_each ( |_| {
9
+ result <<= 1 ;
10
+ result |= n & 1 ;
11
+ n >>= 1 ;
12
+ } ) ;
13
+ result
14
+ }
15
+ }
16
+
17
+ pub struct Solution2 { }
18
+ impl Solution2 {
19
+ pub fn reverse_bits ( mut n : u32 ) -> u32 {
20
+ let mut result: u32 = 0 ;
21
+ for _ in 0 ..32 {
9
22
result <<= 1 ;
10
23
result |= n & 1 ;
11
24
n >>= 1 ;
@@ -21,13 +34,26 @@ mod tests {
21
34
#[ test]
22
35
fn test_reverse_bits ( ) {
23
36
assert_eq ! (
24
- Solution :: reverse_bits(
37
+ Solution1 :: reverse_bits(
38
+ u32 :: from_str_radix( "00000010100101000001111010011100" , 2 ) . unwrap( )
39
+ ) ,
40
+ u32 :: from_str_radix( "00111001011110000010100101000000" , 2 ) . unwrap( )
41
+ ) ;
42
+ assert_eq ! (
43
+ Solution1 :: reverse_bits(
44
+ u32 :: from_str_radix( "11111111111111111111111111111101" , 2 ) . unwrap( )
45
+ ) ,
46
+ u32 :: from_str_radix( "10111111111111111111111111111111" , 2 ) . unwrap( )
47
+ ) ;
48
+
49
+ assert_eq ! (
50
+ Solution2 :: reverse_bits(
25
51
u32 :: from_str_radix( "00000010100101000001111010011100" , 2 ) . unwrap( )
26
52
) ,
27
53
u32 :: from_str_radix( "00111001011110000010100101000000" , 2 ) . unwrap( )
28
54
) ;
29
55
assert_eq ! (
30
- Solution :: reverse_bits(
56
+ Solution2 :: reverse_bits(
31
57
u32 :: from_str_radix( "11111111111111111111111111111101" , 2 ) . unwrap( )
32
58
) ,
33
59
u32 :: from_str_radix( "10111111111111111111111111111111" , 2 ) . unwrap( )
Original file line number Diff line number Diff line change
1
+ // Time: O(n)
2
+ // Space: O(1)
3
+
4
+ pub struct Solution1 { }
5
+ impl Solution1 {
6
+ pub fn single_number ( nums : Vec < i32 > ) -> Vec < i32 > {
7
+ // Xor all the elements to get x ^ y.
8
+ let x_xor_y: i32 = nums. iter ( ) . fold ( 0 , |acc, & num| acc ^ num) ;
9
+
10
+ // Get the last bit where 1 occurs by "x & ~(x - 1)"
11
+ // Because -(x - 1) = ~(x - 1) + 1 <=> -x = ~(x - 1)
12
+ // So we can also get the last bit where 1 occurs by "x & -x"
13
+ let bit: i32 = x_xor_y & -x_xor_y;
14
+ let mut result = vec ! [ 0 ; 2 ] ;
15
+ nums. iter ( )
16
+ . for_each ( |i| result[ ( ( i & bit) != 0 ) as usize ] ^= i) ;
17
+ result
18
+ }
19
+ }
20
+
21
+ pub struct Solution2 { }
22
+ impl Solution2 {
23
+ pub fn single_number ( nums : Vec < i32 > ) -> Vec < i32 > {
24
+ // Xor all the elements to get x ^ y.
25
+ let mut x_xor_y: i32 = 0 ;
26
+ for i in nums. iter ( ) {
27
+ x_xor_y ^= i;
28
+ }
29
+
30
+ // Get the last bit where 1 occurs.
31
+ let bit: i32 = x_xor_y & -x_xor_y;
32
+
33
+ // Get the subset of A where the number has the bit.
34
+ // The subset only contains one of the two integers, call it x.
35
+ // Xor all the elements in the subset to get x.
36
+ let mut x: i32 = 0 ;
37
+ nums. iter ( ) . for_each ( |i| {
38
+ if i & bit != 0 {
39
+ x ^= i;
40
+ }
41
+ } ) ;
42
+ vec ! [ x, x_xor_y ^ x]
43
+ }
44
+ }
45
+
46
+ #[ cfg( test) ]
47
+ mod tests {
48
+ use super :: * ;
49
+
50
+ #[ test]
51
+ fn test_single_number ( ) {
52
+ assert_eq ! ( Solution1 :: single_number( vec![ 1 , 2 , 1 , 3 , 2 , 5 ] ) , vec![ 5 , 3 ] ) ;
53
+
54
+ assert_eq ! ( Solution2 :: single_number( vec![ 1 , 2 , 1 , 3 , 2 , 5 ] ) , vec![ 3 , 5 ] ) ;
55
+ }
56
+ }
You can’t perform that action at this time.
0 commit comments