描述
求一个int类型数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1
数据范围:数据组数:1≤𝑡≤5 1≤t≤5 ,1≤𝑛≤500000 1≤n≤500000
进阶:时间复杂度:𝑂(𝑙𝑜𝑔𝑛) O(logn) ,空间复杂度:𝑂(1) O(1)
输入描述:
输入一个int类型数字
输出描述:
输出转成二进制之后连续1的个数
示例1
输入:
200
输出:
2
说明:
200的二进制表示是11001000,最多有2个连续的1。
num=int(input())
numb=bin(num)[2:]
lengthlst=[]
for i in range(len(numb)):
startindex=i
nowlength=0
while startindex<len(numb) and numb[startindex]=='1':
nowlength+=1
startindex+=1
lengthlst.append(nowlength)
print(max(lengthlst))