779. K-th Symbol in Grammar Medium
1/**
2 * [779] K-th Symbol in Grammar
3 *
4 * We build a table of n rows (1-indexed). We start by writing 0 in the 1^st row. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10.
5 *
6 * For example, for n = 3, the 1^st row is 0, the 2^nd row is 01, and the 3^rd row is 0110.
7 *
8 * Given two integer n and k, return the k^th (1-indexed) symbol in the n^th row of a table of n rows.
9 *
10 * Example 1:
11 *
12 * Input: n = 1, k = 1
13 * Output: 0
14 * Explanation: row 1: <u>0</u>
15 *
16 * Example 2:
17 *
18 * Input: n = 2, k = 1
19 * Output: 0
20 * Explanation:
21 * row 1: 0
22 * row 2: <u>0</u>1
23 *
24 * Example 3:
25 *
26 * Input: n = 2, k = 2
27 * Output: 1
28 * Explanation:
29 * row 1: 0
30 * row 2: 0<u>1</u>
31 *
32 *
33 * Constraints:
34 *
35 * 1 <= n <= 30
36 * 1 <= k <= 2^n - 1
37 *
38 */
39pub struct Solution {}
40
41// problem: https://leetcode.com/problems/k-th-symbol-in-grammar/
42// discuss: https://leetcode.com/problems/k-th-symbol-in-grammar/discuss/?currentPage=1&orderBy=most_votes&query=
43
44// submission codes start here
45
46impl Solution {
47 pub fn kth_grammar(n: i32, k: i32) -> i32 {
48 0
49 }
50}
51
52// submission codes end
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_779() {
60 }
61}
62
Back
© 2025 bowen.ge All Rights Reserved.