Class Solution
-
- All Implemented Interfaces:
public final class Solution1545 - Find Kth Bit in Nth Binary String.
Medium
Given two positive integers
nandk, the binary string <code>S<sub>n</sub></code> is formed as follows:<code>S<sub>1</sub> = "0"</code>
<code>S<sub>i</sub> = S<sub>i - 1</sub> + "1" + reverse(invert(S<sub>i - 1</sub>))</code> for
i > 1
Where
+denotes the concatenation operation,reverse(x)returns the reversed stringx, andinvert(x)inverts all the bits inx(0changes to1and1changes to0).For example, the first four strings in the above sequence are:
<code>S<sub>1</sub> = "0"</code>
<code>S<sub>2</sub> = "011"</code>
<code>S<sub>3</sub> = "0111001"</code>
<code>S<sub>4</sub> = "011100110110001"</code>
Return the <code>k<sup>th</sup></code> bit in <code>S<sub>n</sub></code>. It is guaranteed that
kis valid for the givenn.Example 1:
Input: n = 3, k = 1
Output: "0"
Explanation: S<sub>3</sub> is "0111001". The 1<sup>st</sup> bit is "0".
Example 2:
Input: n = 4, k = 11
Output: "1"
Explanation: S<sub>4</sub> is "011100110110001". The 11<sup>th</sup> bit is "1".
Constraints:
1 <= n <= 20<code>1 <= k <= 2<sup>n</sup> - 1</code>
-
-
Constructor Summary
Constructors Constructor Description Solution()
-
Method Summary
Modifier and Type Method Description final CharacterfindKthBit(Integer n, Integer k)-
-
Method Detail
-
findKthBit
final Character findKthBit(Integer n, Integer k)
-
-
-
-