Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fb33f2

Browse files
author
guangsheng.li01
committed
Solved 0779
1 parent d053c60 commit 1fb33f2

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

‎src/a0779_k_th_symbol_in_grammar.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

‎src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod a0654_maximum_binary_tree;
3636
mod a0701_insert_into_a_binary_search_tree;
3737
mod a0726_number_of_atoms;
3838
mod a0761_special_binary_string;
39+
mod a0779_k_th_symbol_in_grammar;
3940
mod a0783_minimum_distance_between_bst_nodes;
4041
mod a0867_transpose_matrix;
4142
mod a0883_projection_area_of_3d_shapes;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /