【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛
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 <= S.length <= 10001 <= 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)的更多相关文章
- 【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 ...
- #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 ...
- [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 ...
- 【LeetCode】378. Kth Smallest Element in a Sorted Matrix 解题报告(Python)
[LeetCode]378. Kth Smallest Element in a Sorted Matrix 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II 解题报告(Python)
[LeetCode]154. Find Minimum in Rotated Sorted Array II 解题报告(Python) 标签: LeetCode 题目地址:https://leetco ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】107. Binary Tree Level Order Traversal II 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:迭代 日期 [LeetCode ...
随机推荐
- [linux] 常用命令及参数-2
sort 1 sort是把结果输出到标准输出,因此需要输出重定向将结果写入文件 2 sort seq.txt > file.txt 3 sort -u seq.txt 输出去重重复后的行 4 s ...
- Oracle——listener数据库监听 lsnrctl
lsnrctl(Listener Control)是一个SQL*Net工具,用于控制数据库listener,这个工具提供了命令用于控制listener的启动.停止,查看listener的状态,改变li ...
- 安装octave详解
1. 一些可以替换的库(可跳过) 默认的库安装libblas.dll.OpenBLAS-v2.6.0-0-54e7b37_dynamicarch_nt4(自动检测CPU类型) 在目录下<your ...
- 【模板】网络最大流(EK、Dinic、ISAP)(网络流)/洛谷P3376
题目链接 https://www.luogu.com.cn/problem/P3376 题目大意 输入格式 第一行包含四个正整数 \(n,m,s,t\),分别表示点的个数.有向边的个数.源点序号.汇点 ...
- 大数据学习day31------spark11-------1. Redis的安装和启动,2 redis客户端 3.Redis的数据类型 4. kafka(安装和常用命令)5.kafka java客户端
1. Redis Redis是目前一个非常优秀的key-value存储系统(内存的NoSQL数据库).和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list ...
- 从jvm字节码指令看i=i++和i=++i的区别
1. 场景的产生 先来看下下面代码展示的两个场景 @Testvoid testIPP() { int i = 0; for (int j = 0; j < 10; j++) { i = i++; ...
- Oracle—回车、换行符
1.回车换行符 chr(10)是换行符, chr(13)是回车, 增加换行符: select ' update ' || table_name || ' set VALID_STATE =''0A'' ...
- 转Android Canvas和Paint基本使用
Android Canvas和Paint基本使用 这篇文章主要介绍下画笔Paint和画布Canvas的基本使用 1.Paint 创建对象Paint mPaint = new Paint(); 常 ...
- jmeter进阶
1.如果(if)控制器的使用 2.参数的调用 3.数据库的链接
- RAC中常见的高级用法-bind方法
RAC操作思想: Hook(钩子)思想 RAC核心方法:bind bind方法 假设想监听文本框的内容,并且在每次输出结果的时候,都在文本框的内容拼接一段文字" ...