779. K-th Symbol in Grammar

Posted The Tech Road

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了779. K-th Symbol in Grammar相关的知识,希望对你有一定的参考价值。

https://leetcode.com/problems/k-th-symbol-in-grammar/description/

class Solution {
public:
    int kthGrammar(int N, int K, bool rootZero = true) {
        if (N == 1)     return rootZero ? 0 : 1;
        int lastCnt = 1 << (N-2);
        if (K <= lastCnt) {
            return kthGrammar(N-1, K, rootZero);
        }
        else {
            return kthGrammar(N-1, K-lastCnt, !rootZero);
        }
    }
};

 

以上是关于779. K-th Symbol in Grammar的主要内容,如果未能解决你的问题,请参考以下文章

[leetcode-779-K-th Symbol in Grammar]

leetcode 779. K-th Symbol in Grammar ---回溯

K-th Symbol in Grammar

[LeetCode] K-th Symbol in Grammar 语法中的第K个符号

Leetcode: K-th Smallest in Lexicographical Order

[LeetCode] K-th Smallest in Lexicographical Order 字典顺序的第K小数字