题目
解法:递归
class Solution:
def findKthBit(self, n: int, k: int) -> str:
# if n=1, meaning s='0'
if n==1:
return '0'
# compute length of current string
lenth = 2**n - 1
# if k at the middle pos of current string, must be 1
if k==lenth//2+1:
return '1'
# if k at the left half of the current string, continue recursion without reverse and inverse
elif k<lenth//2+1:
return self.findKthBit(n-1,k)
# if k at the right half of the current string, continue recursion with reverse and inverse
else:
# perform reverse position
kk = k-lenth//2 - 1
kk = lenth//2-kk+1
# return inverse of the answer
if self.findKthBit(n-1,kk) == '0':
return '1'
else:
return '0'