Find Kth Bit in Nth Binary String(C++找出第 N 个二进制字符串中的第 K 位)
(1)模拟法
class Solution {
public:
string helper(string s) {
for(int i=0;i<s.length();i++) {
if(s[i]=='0') s[i]='1';
else s[i]='0';
}
reverse(s.begin(),s.end());
return s;
}
char findKthBit(int n, int k) {
string s="0";
while(n>1) {
s=s+"1"+helper(s);
n--;
}
return s[k-1];
}
};
还没有评论,来说两句吧...