题目如下:

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. form表单action带参数传递

    form表单action带参数传递function submit(){  var u = document.forms[0].elements["user"].value;  do ...

  2. (转)k8s集群部署二:flannel网络

    转:https://blog.csdn.net/sinat_35930259/article/details/79946146 Overlay Network模式 覆盖网络,在基础网络上叠加的一种虚拟 ...

  3. selenium设定请求头

    如果只是需要设定User-Agent可以用如下所示方式 dcap = dict(DesiredCapabilities.PHANTOMJS)dcap["phantomjs.page.sett ...

  4. 设置请求头信息User-Agent

    设置请求头信息User-Agent来模拟浏览器. 先来看User-Agent: 当我们向服务器发送请求时,浏览器会将一些头信息附加上,然后发给服务器. 如上图所示头信息(请求头信息 Request H ...

  5. EZOJ #389点分治好题

    分析 一层一层把叶子去掉 看最多能去掉多少层即可 代码 #include<bits/stdc++.h> using namespace std; ],du[],fa[],n,m,ans; ...

  6. JS-递归获取当前节点全部指定类型的子节点

    在线预览 方法 使用 nodeType 判断类型,在 allChildNodes 方法内建立递归函数将 allCN 封装在方法内. <!DOCTYPE html> <html lan ...

  7. leetcode 189. 旋转数组(python)

    给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6,7] 和 k = 3输出: [5,6,7,1,2,3,4]解释:向右旋转 1 步: ...

  8. spring4.1.8扩展实战之四:感知spring容器变化(SmartLifecycle接口)

    本章是<spring4.1.8扩展实战>的第四篇,如果业务上需要在spring容器启动和关闭的时候做一些操作,可以自定义SmartLifecycle接口的实现类来扩展,本章我们通过先分析再 ...

  9. Pikachu漏洞练习平台实验——不安全的文件下载和上传(七)

    1.不安全的文件下载 1.1.概述 文件下载功能在很多web系统上都会出现,一般我们当点击下载链接,便会向后台发送一个下载请求,一般这个请求会包含一个需要下载的文件名称,后台在收到请求后 会开始执行下 ...

  10. js 函数 写法

    // function ckeckName(){}; // function checkUser(){}; // function checkPassWorld(){}; // var checkNa ...