c语言sscanf函数的用法是什么
374
2022-09-14
[leetcode] 1016. Binary String With Substrings Representing 1 To N
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 = 3Output: true
Example 2:
Input: S = "0110", N = 4Output: false
Note:
1 <= S.length <= 10001 <= 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
参考文献
[LeetCode] Python Straightforward
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~