题目如下:

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. 1 <= S.length <= 1000
  2. 1 <= N <= 10^9

解题思路:最初我的想法是判断1~N是否存在于S中,但是这样效率太低。思路转换一下,判断S中能否组成从1~N中所有的数即可,

代码如下:

class Solution(object):
def queryString(self, S, N):
"""
:type S: str
:type N: int
:rtype: bool
"""
dic = {}
for i in range(len(S)-1,-1,-1):
power = 0
amount = 0
for j in range(i,-1,-1):
if S[j] == '':
power += 1
continue
tmp = amount + pow(2, power)
if tmp > N:
break
dic[tmp] = 1
power += 1
amount = tmp
return len(dic) == N

【leetcode】1023. Binary String With Substrings Representing 1 To N的更多相关文章

  1. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  4. 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)

    [LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...

  5. 【LeetCode】145. Binary Tree Postorder Traversal

    Difficulty: Hard  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...

  6. 【LeetCode】二叉查找树 binary search tree(共14题)

    链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...

  7. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  8. #Leetcode# 1016. Binary String With Substrings Representing 1 To N

    https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/ Given a binary stri ...

  9. 【LeetCode】Validate Binary Search Tree ——合法二叉树

    [题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...

随机推荐

  1. 重写LayoutParams,读取子View自定义属性

    在EasyConstraintLayout内部定义一个静态类LayoutParams继承ConstraintLayout.LayoutParams,然后在构造方法中读取上面自定义的属性.我们通过裁剪的 ...

  2. UVA 11752 The Super Powers(暴力)

    题目:https://cn.vjudge.net/problem/UVA-11752 题解:这里只讨论处理越界的问题. 因为题目最上界是 264-1. 我们又是求次幂的. 所以当我们就可以知道 i 的 ...

  3. 解决Docker容器 iptables问题---docker: Error response from daemon: driver failed programming external connectivity on endpoint quizzical_thompson

    一.问题现象 最近在研究Docker容器日志管理时,启动容器出现iptables相关报错,具体问题如下 运行容器 [root@node-11 ~]# docker run -d -p 24224:24 ...

  4. ruby在类中访问@,类外访问调用方法

    class Box def initialize(w,h) @width,@height=w,h end def printWidth puts @width end def printHeight ...

  5. java数组,遍历数组

    数组:一组具有相同数据类型的集合(容器) 1.数组声明格式: 数据类型 [] 数组名 = new 数据类型[长度]: 数组长度一旦确定无法更改. 数组里的数据必须是相同类型或自动向上转型后兼容的类型 ...

  6. Win7 VS2019安装后创建C++工程失败解决

    VS2019正式上手,第1个问题创建不了工程,看起来非常类似之前VS2017更新 解决办法 https://github.com/Microsoft/msbuild/issues/4286 和上次的问 ...

  7. Git013--多人协作

    Git--多人协作 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/ ...

  8. Mac003--Maven安装与环境变量配置

    Mac--Maven安装 一.应用brew安装maven及安装位置 打开终端,输入命令:brew install maven 参考博客:https://www.jianshu.com/p/230e0b ...

  9. Object.watch

    /*  * object.watch polyfill  *  * 2012-04-03  *  * By Eli Grey, http://eligrey.com  * Public Domain. ...

  10. [SDOI2019]快速查询

    [SDOI2019]快速查询 [题目链接] 链接 [思路要点] 据说是 \(\text{SDOI2019}\) 最水的题 操作次数为 \(1e7\) 范围,显然要求每次操作 \(\mathcal{O} ...