File tree 2 files changed +35
-0
lines changed
2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * [0779] k-th-symbol-in-grammar
3
+ */
4
+
5
+ struct Solution ;
6
+
7
+ impl Solution {
8
+ pub fn kth_grammar ( n : i32 , k : i32 ) -> i32 {
9
+ let mut k = k;
10
+ let mut n = n - 1 ;
11
+ let mut p = false ;
12
+ while n > 0 {
13
+ n -= 1 ;
14
+ if k % 2 == 0 {
15
+ p = !p;
16
+ }
17
+ k = ( k - 1 ) / 2 + 1 ;
18
+ }
19
+ p as i32
20
+ }
21
+ }
22
+
23
+ #[ cfg( test) ]
24
+ mod tests {
25
+ use super :: * ;
26
+
27
+ #[ test]
28
+ fn test_case0 ( ) {
29
+ assert_eq ! ( Solution :: kth_grammar( 1 , 1 ) , 0 ) ;
30
+ assert_eq ! ( Solution :: kth_grammar( 2 , 1 ) , 0 ) ;
31
+ assert_eq ! ( Solution :: kth_grammar( 2 , 2 ) , 1 ) ;
32
+ assert_eq ! ( Solution :: kth_grammar( 4 , 5 ) , 1 ) ;
33
+ }
34
+ }
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ mod a0654_maximum_binary_tree;
36
36
mod a0701_insert_into_a_binary_search_tree;
37
37
mod a0726_number_of_atoms;
38
38
mod a0761_special_binary_string;
39
+ mod a0779_k_th_symbol_in_grammar;
39
40
mod a0783_minimum_distance_between_bst_nodes;
40
41
mod a0867_transpose_matrix;
41
42
mod a0883_projection_area_of_3d_shapes;
You can’t perform that action at this time.
0 commit comments