Last active
August 25, 2019 21:10
-
-
Save yanhsiah/6c2ff2732ca896a04fd770a54022ffad to your computer and use it in GitHub Desktop.
779. K-th Symbol in Grammar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int kthGrammar(int N, int K) { | |
if (N == 1 && K == 1) return 0; | |
if (K % 2) { | |
return kthGrammar(N - 1, (K + 1) / 2) ? 1 : 0; | |
} else { | |
return kthGrammar(N - 1, (K + 1) / 2) ? 0 : 1; | |
} | |
/* | |
value: 0 | |
index: 1 | |
value: 01 | |
index: 1 | |
value: 01 10 | |
index: 2 | |
value: 01 10 10 01 | |
index: 4 | |
value: 01 10 10 01 10 01 01 10 | |
index: 78 | |
*/ | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment