【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—查看内核版本、系统版本、系统位数
一.查看内核版本命令: 1) [root@q1test01 ~]# cat /proc/version Linux version 2.6.9-22.ELsmp (bhcompile@crowe. ...
- 12-Add Digits
寻找一个数的数根,用了暴力破解的方式,时间复杂度比较高 暂未想到O(1)的方式 Given a non-negative integer num, repeatedly add all its dig ...
- 进程和线程操作系统转载的Mark一下
https://www.cnblogs.com/leisure_chn/p/10393707.html Linux的进程线程及调度 本文为宋宝华<Linux的进程.线程以及调度>学习笔记. ...
- 以DevExpress开发的WinFrom程序的多语言功能的实现
以DevExpress开发的WinFrom程序的多语言功能的实现 写在前面: 多语言切换功能在Winform程序中是经常遇到的需求,尤其是需要给国外客户使用的情况下,多语言功能是必不可少的.前一段时间 ...
- 07 MySQL安装图解--Windows版本
MySQL安装图解 使用微信扫码关注微信公众号,并回复:"MySQL环境",免费获取下载链接! 1.安装MySQL 2.校验MySQL 3.登录MySQL 登录MySQL:mysq ...
- 分布式事务(4)---最终一致性方案之TCC
分布式事务(1)-理论基础 分布式事务(2)---强一致性分布式事务解决方案 分布式事务(3)---强一致性分布式事务Atomikos实战 强一致性分布式事务解决方案要求参与事务的各个节点的数据时刻保 ...
- h5移动端设备像素比dpr介绍
首先介绍一下概念 devicePixelRatio其实指的是window.devicePixelRatio window.devicePixelRatio是设备上物理像素和设备独立像素(device- ...
- Oracle中的job(定时任务)
oracle中的job(定时任务)由dbms_job包下的函数实现.关于job的理论知识可参考https://blog.csdn.net/apextrace/article/details/77675 ...
- [学习总结]8、android 自定义控件 使用declare-styleable进行配置属性(源码角度)
declare-styleable:declare-styleable是给自定义控件添加自定义属性用的. 官方的相关内部控件的配置属性文档:http://developer.android.com/r ...
- Linux基础命令---nfsstat显示nfs信息
nfsstat nfsstat指令用来显示nfs客户端和服务器的活动信息. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.Fedora. 1.语法 nfsstat ...