【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 ...
随机推荐
- 170821-关于SpringMVC的知识点
1.SpringMVC 概述以及优势 SpringMVC和Spring的关系: 软件开发的三层架构: web层[表示层.表现层]---->Service层---->Dao[DataBas ...
- 破解Revealapp的试用时间限制
转载自:http://jingwei6.me/2014/02/28/reveal_crack.html Revealapp作为分析iOS app UI结构的利器,还是非常称手的,89刀的价格也是物有所 ...
- (转)Hyper-v 安装CentOS 7 (其他虚拟机一样参考)
转:https://www.cnblogs.com/dunitian/p/4976077.html 平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunit ...
- 【CDN+】 CDN项目的两大核心--缓存与回源
前言 项目中碰到CDN专用名词: 回源, 然后不知道什么意思,反过来查询了一下CDN相关的一些基本术语,特做记录 CDN基础概念 CDN (Content Delivery Network,即内容分发 ...
- Skyline(6.x)-二次开发手册使用技巧
毕业设计选择 Skyline 的 Web 端二次开发,由于以前没有接触过 ActiveX 控件的使用,二次开发手册是英文的读起来有点吃力,并且 IE 直接控制台输出 ActiveX 控件创建的对象看不 ...
- 002-Visio绘制时序图
一.概述 1.1.什么时候使用 当编码的时候,知道有的用例的业务逻辑按照比较确定的时间先后顺序进行展开.这时候,我们就需要知道我们设计的系统中的不同类之间传递消息(可以认为是不同对象函数间的调用)要按 ...
- AppiumLibrary库倒入后显示红色,日志报错:ImportError: cannot import name 'InvalidArgumentException'
AppiumLibrary安装后,robotframe worke 倒入后一直显示红色,查看日志报错:ImportError: cannot import name 'InvalidArgumentE ...
- workflow-core 简介
最近想做一个OA相关的网站开发,一直都听说有workflow的东西,之前也断断续续学习过 Workflow Foundation 4.0,还是没有搞明白到底能够用它做什么 但还是觉得workflow在 ...
- mysql解析json字符串相关问题
很多时候,我们需要在sql里面直接解析json字符串.这里针对mysql5.7版本的分水岭进行区分. 1.对于mysql5.7以上版本 使用mysql的内置函数JSON_EXTRACT(column, ...
- 解读vue filter
1.全局filter, 全局的过滤一般在main.js里面使用 <div id="app"> <div> {{testVal | filVal(10,30) ...