class Solution {
public:
int kthGrammar(int n, int k) {
//第n行共有2^(n - 1)个数
//如果k > 2^(n - 1) / 2 说明在后半段, 此时可以转换为前半段
//即k = k - 2^(n - 1) / 2, 此时需要换一次方向
//否则 k = k;无需修改方向
//最后蒋规模缩小一半,直至规模为1
int s = 2 << (n - 1);
int turn_count = 0;
int res = -1;
while (s > 1) {
s >>= 1;
if (k > s) {
k -= s;
turn_count ++;
}
}
if (turn_count % 2) res = 1;
else res = 0;
return res;
}
};
01-02
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-20
274
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-22
5721
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-06
2340
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-26
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交