【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 integerN, 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: trueExample 2:
Input: S = "0110", N = 4
Output: falseNote:
1 <= S.length <= 10001 <= 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的更多相关文章
- 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】199. Binary Tree Right Side View 解题报告(Python)
[LeetCode]199. Binary Tree Right Side View 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/probl ...
- 【LeetCode】145. Binary Tree Postorder Traversal
Difficulty: Hard More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/binary-tree-pos ...
- 【LeetCode】二叉查找树 binary search tree(共14题)
链接:https://leetcode.com/tag/binary-search-tree/ [220]Contains Duplicate III (2019年4月20日) (好题) Given ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- #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 ...
- 【LeetCode】Validate Binary Search Tree ——合法二叉树
[题目] Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defin ...
随机推荐
- Hibernate入门学习笔记
1.Hibernate是什么? 2.hibernate怎么配置? 3.SessionFactory是干什么的?有哪些方法经常用? 4.hibernate的现成的增删改查方法怎么使用?都有哪些方法?哪些 ...
- php面试专题---14、Linux基础考点
php面试专题---14.Linux基础考点 一.总结 一句话总结: php考linux其实也考不了很难 1.系统定时任务? crontab命令和 at命令 crontab命令 crontab -e ...
- day17—Flex弹性布局详解(一)
转行学开发,代码100天——2018-04-02 今天看到一篇大神的文章,关于flex布局的详解,对flex用法介绍的相当详细,非常有助于我等初学者更深入了解这种布局方式. 文章链接 [基础知识]Fl ...
- maven 依赖调解
项目A有两条依赖关系 A->B->C->X(1.0),A->D->X(2.0) ,X是A的传递性依赖,但是两条路径上有两个版本的依赖,会选择哪个呢? maven 依赖调 ...
- WEB服务端安全---认证会话与访问控制
一.认证与会话管理 认证:简而言之就是通过一定的凭证认出用户是谁.认证过程中按凭证数量可简单分为 ‘单因素认证’.‘双因素认证’或多因素认证.一般来说,多因素认证强度更高,但是用户体验上会比单因素认证 ...
- SQLMap使用总结
支持模式:布尔/时间/报错/联合查询/堆查询 支持数据库:MySQL, Oracle, PostgreSQL, Microsoft SQL Server, Microsoft Access, IBM ...
- mysql 5.1.34
在make之前,将MAKEFILE中的do abi check注释,不要注释名字... mysql 5.1 编译安装 分类: mysql2012-04-06 13:01 17175人阅读 评论(0) ...
- ruby基本语法(1)
一些学习资源 http://www.codecademy.com/zh/courses/ruby-beginner-en-d1Ylq/0/5?curriculum_id=5059f8619189a50 ...
- mybatis多对多
这里我们以用户 user 表和 角色role 表为例,假定一个用户能被分配成多重角色,而一种角色也能分给多个用户,故用户和角色构成多对多的关系 需求:给定角色id,查询这个角色所属的所有用户信息 ①. ...
- Tensorflow--Keras官方原文
Keras 是一个用于构建和训练深度学习模型的高阶 API(应用程序接口).它可用于快速设计原型.高级研究和生产,具有以下三个主要优势: 方便用户使用 Keras 具有针对常见用例做出优化的简单而一致 ...