作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/binary-string-with-substrings-representing-1-to-n/

题目描述

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

题目大意

判断小于等于N的所有正整数,其二进制是否都在S中出现。

解题方法

还是看Note,我们发现S的长度不算太长,但是N的范围倒是挺大的。如果暴力从1循环到N进行查找判断,一定会超时。

这个时候就来了个技巧:倒序遍历!我们判断从N到1是否都在S中出现。

109的二进制有30位,而109的二进制和10^9 - 1的二进制分别如下:

bin(10**9) = '0b111011100110101100101000000000'
bin(10**9 - 1) = '0b111011100110101100100111111111'

可以看到两者的二进制的前半部分相等,而最后部分存在差异。那么,我们考虑一下,如果bin(10**9)在S中出现了,那么bin(10**9 - 1)也在S中出现的概率大吗?很明显这个概率应该是很小的,哪怕同时出现了,对于S只有长度1000来说,顶多也只能存在30个左右的不同32位的二进制字符串。

所以,这种先判断大的数字、长的二进制表示的字符串是否在S中出现的策略效率是非常高的。相反如果从1,2,3……这些数字从小到大进行判断,那么每个数字出现的概率都很大,需要找更多的数字才行,很可能会超时。

Python代码如下:

class Solution(object):
def queryString(self, S, N):
"""
:type S: str
:type N: int
:rtype: bool
"""
return all(bin(n)[2:] in S for n in range(N, 0, -1))

参考资料:https://leetcode.com/problems/best-sightseeing-pair/discuss/260850/JavaC%2B%2BPython-One-Pass

日期

2019 年 3 月 24 日 —— 这个周赛太悲催了

【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)的更多相关文章

  1. 【leetcode】1023. Binary String With Substrings Representing 1 To N

    题目如下: Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, r ...

  2. #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 ...

  3. [Swift]LeetCode1016. 子串能表示从 1 到 N 数字的二进制串 | Binary String With Substrings Representing 1 To N

    Given a binary string S (a string consisting only of '0' and '1's) and a positive integer N, return ...

  4. 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)

    [LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  5. 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)

    [LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...

  6. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

  7. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  8. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

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

  9. 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...

随机推荐

  1. python3安装,支持openssl,支持采集https

    python3安装,支持openssl,支持采集https 坑好多,特别是安装的时候,各种不匹配,服务器默认配置是python2,升级3后,采集的时候用到openssl,花了两天也没搞定各种错误,也许 ...

  2. 爬虫动态渲染页面爬取之selenium驱动chrome浏览器的使用

    Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样,可以用其进行网页动态渲染页面的爬取. 支持的浏览器包括IE(7, 8, 9, 10 ...

  3. ubuntu常见错误--Could not get lock /var/lib/dpkg/lock

    ubuntu常见错误--Could not get lock /var/lib/dpkg/lock   通过终端安装程序sudo apt-get install xxx时出错:   E: Could ...

  4. ache

    ache和pain可能没啥差别,头疼和头好痛都对.从词典来看,有backache, bellyache, earache, headache, heartache, moustache/mustach ...

  5. CSS系列,三栏布局的四种方法

    三栏布局.两栏布局都是我们在平时项目里经常使用的,今天我们来玩一下三栏布局的四种写法,以及它的使用场景. 所谓三栏布局就是指页面分为左中右三部分然后对中间一部分做自适应的一种布局方式. 1.绝对定位法 ...

  6. 队列——Java实现

    1 package struct; 2 3 interface IQueue{ 4 //入队列 5 void add(Object obj); 6 //出队列 7 Object remove(); 8 ...

  7. 【Linux】【Services】【SaaS】Docker+kubernetes(1. 基础概念与架构图)

    1.简介 1.1.  背景:公司正在进行敏捷开发环境的搭建,以取代传统的架构,好处大大的,我就不赘述了.公司原来负责这个项目的同事要转组,我只好交给另外同事继续,但是为了防止同样的事情,我也需要深入了 ...

  8. 1.Java语言基础

    一:java语言介绍 (1). 1991年出现,1995年5月正式发布 出生地:SUN  创始人:James Gosling  2009年4月被Oracle收购 目前最新的版本2018年3月v10.0 ...

  9. 一个超简单的Microsoft Edge Extension

    这个比微软官网上的例子简单很多,适合入门.总共4个文件: https://files.cnblogs.com/files/blogs/714801/cet6wordpicker.zip 36KB 1. ...

  10. shell脚本 安全删除MySQL大表

    一.简介 源码地址 日期:2018/4/12 介绍:工具用于安全删除MySQL表,对于一些特定场景可能有用 应用场景:大批删除不走索引但是有主键的场景[可以是单列索引,也可是多列索引] 实现思路:根据 ...