File tree 2 files changed +50
-1
lines changed
2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,358 LeetCode solutions in JavaScript
1
+ # 1,359 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcodejavascript.com ] ( https://leetcodejavascript.com )
4
4
1180
1180
1541|[ Minimum Insertions to Balance a Parentheses String] ( ./solutions/1541-minimum-insertions-to-balance-a-parentheses-string.js ) |Medium|
1181
1181
1542|[ Find Longest Awesome Substring] ( ./solutions/1542-find-longest-awesome-substring.js ) |Hard|
1182
1182
1544|[ Make The String Great] ( ./solutions/1544-make-the-string-great.js ) |Easy|
1183
+ 1545|[ Find Kth Bit in Nth Binary String] ( ./solutions/1545-find-kth-bit-in-nth-binary-string.js ) |Medium|
1183
1184
1550|[ Three Consecutive Odds] ( ./solutions/1550-three-consecutive-odds.js ) |Easy|
1184
1185
1551|[ Minimum Operations to Make Array Equal] ( ./solutions/1551-minimum-operations-to-make-array-equal.js ) |Medium|
1185
1186
1566|[ Detect Pattern of Length M Repeated K or More Times] ( ./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1545. Find Kth Bit in Nth Binary String
3
+ * https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given two positive integers n and k, the binary string Sn is formed as follows:
7
+ * - S1 = "0"
8
+ * - Si = Si - 1 + "1" + reverse(invert(Si - 1)) for i > 1
9
+ *
10
+ * Where + denotes the concatenation operation, reverse(x) returns the reversed string x, and
11
+ * invert(x) inverts all the bits in x (0 changes to 1 and 1 changes to 0).
12
+ *
13
+ * For example, the first four strings in the above sequence are:
14
+ * - S1 = "0"
15
+ * - S2 = "011"
16
+ * - S3 = "0111001"
17
+ * - S4 = "011100110110001"
18
+ *
19
+ * Return the kth bit in Sn. It is guaranteed that k is valid for the given n.
20
+ */
21
+
22
+ /**
23
+ * @param {number } n
24
+ * @param {number } k
25
+ * @return {character }
26
+ */
27
+ var findKthBit = function ( n , k ) {
28
+ let position = k - 1 ;
29
+ let invertCount = 0 ;
30
+ let length = ( 1 << n ) - 1 ;
31
+
32
+ while ( position !== 0 ) {
33
+ const mid = length >> 1 ;
34
+
35
+ if ( position === mid ) {
36
+ return invertCount % 2 === 0 ? '1' : '0' ;
37
+ }
38
+
39
+ if ( position > mid ) {
40
+ position = length - position - 1 ;
41
+ invertCount ++ ;
42
+ } else {
43
+ length = mid ;
44
+ }
45
+ }
46
+
47
+ return invertCount % 2 === 0 ? '0' : '1' ;
48
+ } ;
You can’t perform that action at this time.
0 commit comments