-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cc5ff8
commit f5cee45
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<h2><a href="https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/?envType=daily-question&envId=2024-10-19">1545. Find Kth Bit in Nth Binary String</a></h2><h3>Medium</h3><hr><p>Given two positive integers <code>n</code> and <code>k</code>, the binary string <code>S<sub>n</sub></code> is formed as follows:</p> | ||
|
||
<ul> | ||
<li><code>S<sub>1</sub> = "0"</code></li> | ||
<li><code>S<sub>i</sub> = S<sub>i - 1</sub> + "1" + reverse(invert(S<sub>i - 1</sub>))</code> for <code>i > 1</code></li> | ||
</ul> | ||
|
||
<p>Where <code>+</code> denotes the concatenation operation, <code>reverse(x)</code> returns the reversed string <code>x</code>, and <code>invert(x)</code> inverts all the bits in <code>x</code> (<code>0</code> changes to <code>1</code> and <code>1</code> changes to <code>0</code>).</p> | ||
|
||
<p>For example, the first four strings in the above sequence are:</p> | ||
|
||
<ul> | ||
<li><code>S<sub>1 </sub>= "0"</code></li> | ||
<li><code>S<sub>2 </sub>= "0<strong>1</strong>1"</code></li> | ||
<li><code>S<sub>3 </sub>= "011<strong>1</strong>001"</code></li> | ||
<li><code>S<sub>4</sub> = "0111001<strong>1</strong>0110001"</code></li> | ||
</ul> | ||
|
||
<p>Return <em>the</em> <code>k<sup>th</sup></code> <em>bit</em> <em>in</em> <code>S<sub>n</sub></code>. It is guaranteed that <code>k</code> is valid for the given <code>n</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 3, k = 1 | ||
<strong>Output:</strong> "0" | ||
<strong>Explanation:</strong> S<sub>3</sub> is "<strong><u>0</u></strong>111001". | ||
The 1<sup>st</sup> bit is "0". | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> n = 4, k = 11 | ||
<strong>Output:</strong> "1" | ||
<strong>Explanation:</strong> S<sub>4</sub> is "0111001101<strong><u>1</u></strong>0001". | ||
The 11<sup>th</sup> bit is "1". | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= n <= 20</code></li> | ||
<li><code>1 <= k <= 2<sup>n</sup> - 1</code></li> | ||
</ul> |