Python|检查一个二进制数中是否有K个连续的1
给定 K 和一个二进制数,检查二进制数中是否存在 k 个连续的 1。
例子:
Input : binary number = 101010101111
k = 4
Output : yes
Explanation: at the last 4 index there exists
4 consecutive 1's
Input : binary number = 11100000 k=5
Output : no
Explanation: There is a maximum of 3 consecutive
1's in the given binary.
方法:创建一个带有 k 1 的新字符串。使用 if 条件检查 s 中是否有新的。在Python中 if new in s:检查是否存在 if new in s,因此如果存在则返回 true,否则返回 false。
以下是上述方法的Python实现:
# Python program to check if there
# is k consecutive 1's in a binary number
# function to check if there are k
# consecutive 1's
def check(s,k):
# form a new string of k 1's
new = "1"*k
# if there is k 1's at any position
if new in s:
print "yes"
else:
print "no"
# driver code
s = "10101001111"
k = 4
check(s, k)
输出:
yes