Description
Given a binary string S (a string consisting only of ‘0’ and '1’s) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.
Example 1:
Input: S = "0110", N = 3
Output: true
Example 2:
Input: S = "0110", N = 4
Output: false
Note:
- 1 <= S.length <= 1000
- 1 <= N <= 10^9
分析
题目的意思是:给定一个字符串S和数字N,判断1~N的所有数的二进制是否都是字符串S的子串。这道题我看了之后,没有想到其他的捷径,做法很直接,把数字变成二进制进行判断就行了,没想到也ac了。我看了一下其他人的实现,跟我的差不多。
代码
class Solution:
def solve(self,n):
res=[]
while(n>0):
t=n%2
res.append(str(t))
n=n//2
res.reverse()
return ''.join(res)
def queryString(self, S: str, N: int) -> bool:
n=len(S)
for i in range(N):
a=self.solve(i+1)
if(a not in S):
return False
return True