LeetCode 779第K个语法符号(JAVA)
程序员文章站
2024-03-19 20:27:22
...
class Solution {
public int kthGrammar(int n, int k) {
int a=1;int i=0;
for(;;){
if(a>=k){
n=i;
break;
}else{
a*=2;
i++;
}
}
i=0;
while(k>1){
if(k<=a) {a/=2;continue;}
k-=a;
i++;
}
return i%2;
}
}