当前位置:网站首页>LeetCode 1545. Find the k-th bit in the nth binary string
LeetCode 1545. Find the k-th bit in the nth binary string
2022-07-06 16:42:00 【Daylight629】
1545. Find out the N Of the binary strings K position
Here are two positive integers n
and k
, Binary string Sn
The formation rules of are as follows :
S1 = "0"
- When
i > 1
when ,Si = Si-1 + "1" + reverse(invert(Si-1))
among +
Indicates series operation ,reverse(x)
Return to reverse x
The resulting string , and invert(x)
Will flip x Every one of you (0 Turn into 1, and 1 Turn into 0).
for example , The first of the sequences described above 4 The strings are :
S1 = "0"
S2 = "0**1**1"
S3 = "011**1**001"
S4 = "0111001**1**0110001"
Please return Sn
Of The first k
Bit character , Topic data assurance k
It must be Sn
Within the length range .
Example 1:
Input :n = 3, k = 1
Output :"0"
explain :S3 by "0111001", Its first 1 Position as "0" .
Example 2:
Input :n = 4, k = 11
Output :"1"
explain :S4 by "011100110110001", Its first 11 Position as "1" .
Example 3:
Input :n = 1, k = 1
Output :"0"
Example 4:
Input :n = 2, k = 3
Output :"1"
Tips :
1 <= n <= 20
1 <= k <= 2n - 1
Two 、 Method 1
recursive
class Solution {
public char findKthBit(int n, int k) {
if (k == 1) {
return '0';
}
int mid = 1 << (n - 1);
if (k == mid) {
return '1';
} else if (k < mid) {
return findKthBit(n - 1, k);
} else {
k = mid * 2 - k;
return invert(findKthBit(n - 1, k));
}
}
public char invert(char a) {
return (char) ('0' + '1' - a);
}
}
Complexity analysis
Time complexity :O(n). character string S_n The length of is 2^n-1, Each recursive call can reduce the search range by half , So the time complexity is zero O(log2n)=O(n).
Spatial complexity :O(n). The space complexity mainly depends on the stack space generated by recursive calls , The number of recursive call layers will not exceed n.
边栏推荐
- Install Jupiter notebook under Anaconda
- Simply try the new amp model of deepfacelab (deepfake)
- Educational Codeforces Round 130 (Rated for Div. 2)A~C
- Base dice (dynamic programming + matrix fast power)
- 拉取分支失败,fatal: ‘origin/xxx‘ is not a commit and a branch ‘xxx‘ cannot be created from it
- Sublime text code formatting operation
- ffmpeg命令行使用
- The concept of spark independent cluster worker and executor
- JS encapsulates the method of array inversion -- Feng Hao's blog
- 新手必会的静态站点生成器——Gridsome
猜你喜欢
Codeforces Round #801 (Div. 2)A~C
Discussion on QWidget code setting style sheet
Hbuilder x format shortcut key settings
图像处理一百题(11-20)
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
第7章 __consumer_offsets topic
两个礼拜速成软考中级软件设计师经验
Codeforces Round #802(Div. 2)A~D
< li> dot style list style type
解决Intel12代酷睿CPU【小核载满,大核围观】的问题(WIN11)
随机推荐
新手必会的静态站点生成器——Gridsome
JS encapsulates the method of array inversion -- Feng Hao's blog
Research Report of desktop clinical chemical analyzer industry - market status analysis and development prospect prediction
使用jq实现全选 反选 和全不选-冯浩的博客
sublime text 代码格式化操作
How to insert mathematical formulas in CSDN blog
Chapter 1 overview of MapReduce
ffmpeg命令行使用
js时间函数大全 详细的讲解 -----阿浩博客
Tert butyl hydroquinone (TBHQ) Industry Research Report - market status analysis and development prospect forecast
本地可视化工具连接阿里云centOS服务器的redis
Use JQ to realize the reverse selection of all and no selection at all - Feng Hao's blog
Audio and video development interview questions
音视频开发面试题
(lightoj - 1236) pairs forming LCM (prime unique decomposition theorem)
原生js实现全选和反选的功能 --冯浩的博客
Discussion on QWidget code setting style sheet
Tree of life (tree DP)
China tetrabutyl urea (TBU) market trend report, technical dynamic innovation and market forecast
MP4格式详解