72
73
kamyu104/src/binary_watch.rsCopy file name to clipboard +46 Lines changed: 46 additions & 0 deletions Original file line number Diff line number Diff line change
1
+ use std:: collections:: BTreeSet ;
2
+
3
+ // Time: O(1)
4
+ // Space: O(1)
5
+
6
+ pub struct Solution { }
7
+ impl Solution {
8
+ pub fn read_binary_watch ( num : i32 ) -> Vec < String > {
9
+ let mut result: Vec < String > = Vec :: new ( ) ;
10
+ ( 0 ..12 ) . for_each ( |h| {
11
+ ( 0 ..60 ) . for_each ( |m| {
12
+ if ( Solution :: bit_count ( h) + Solution :: bit_count ( m) ) == num {
13
+ result. push ( format ! ( "{}:{:02}" , h, m) ) ;
14
+ }
15
+ } ) ;
16
+ } ) ;
17
+ result
18
+ }
19
+
20
+ fn bit_count ( mut bits : i32 ) -> i32 {
21
+ let mut count: i32 = 0 ;
22
+ while bits != 0 {
23
+ count += 1 ;
24
+ bits &= bits - 1 ;
25
+ }
26
+ count
27
+ }
28
+ }
29
+
30
+ #[ cfg( test) ]
31
+ mod tests {
32
+ use super :: * ;
33
+
34
+ #[ test]
35
+ fn test_read_binary_watch ( ) {
36
+ assert_eq ! (
37
+ Solution :: read_binary_watch( 1 )
38
+ . into_iter( )
39
+ . collect:: <BTreeSet <String >>( ) ,
40
+ vec![ "1:00" , "2:00" , "4:00" , "8:00" , "0:01" , "0:02" , "0:04" , "0:08" , "0:16" , "0:32" ]
41
+ . into_iter( )
42
+ . map( String :: from)
43
+ . collect:: <BTreeSet <String >>( )
44
+ ) ;
45
+ }
46
+ }
Original file line number Diff line number Diff line change
1
+ mod binary_watch;
1
2
mod bitwise_and_of_numbers_range;
2
3
mod find_the_difference;
3
4
mod maximum_product_of_word_lengths;
Original file line number Diff line number Diff line change
1
+ use std:: collections:: BTreeSet ;
2
+
1
3
// Time: O(n)
2
4
// Space: O(1)
3
5
@@ -49,8 +51,18 @@ mod tests {
49
51
50
52
#[ test]
51
53
fn test_single_number ( ) {
52
- assert_eq ! ( Solution1 :: single_number( vec![ 1 , 2 , 1 , 3 , 2 , 5 ] ) , vec![ 5 , 3 ] ) ;
54
+ assert_eq ! (
55
+ Solution1 :: single_number( vec![ 1 , 2 , 1 , 3 , 2 , 5 ] )
56
+ . into_iter( )
57
+ . collect:: <BTreeSet <i32 >>( ) ,
58
+ vec![ 3 , 5 ] . into_iter( ) . collect:: <BTreeSet <i32 >>( )
59
+ ) ;
53
60
54
- assert_eq ! ( Solution2 :: single_number( vec![ 1 , 2 , 1 , 3 , 2 , 5 ] ) , vec![ 3 , 5 ] ) ;
61
+ assert_eq ! (
62
+ Solution2 :: single_number( vec![ 1 , 2 , 1 , 3 , 2 , 5 ] )
63
+ . into_iter( )
64
+ . collect:: <BTreeSet <i32 >>( ) ,
65
+ vec![ 3 , 5 ] . into_iter( ) . collect:: <BTreeSet <i32 >>( )
66
+ ) ;
55
67
}
56
68
}
You can’t perform that action at this time.
0 commit comments