【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 ...
随机推荐
- 一文读懂PID控制算法(抛弃公式,从原理上真正理解PID控制)
PID控制应该算是应用非常广泛的控制算法了.小到控制一个元件的温度,大到控制无人机的飞行姿态和飞行速度等等,都可以使用PID控制.这里我们从原理上来理解PID控制. PID(proportion ...
- HDU6669 Game(思维,贪心)
HDU6669 Game 维护区间 \([l,r]\) 为完成前 \(i\) 步使用最少步数后可能落在的区间. 初始时区间 \([l,r]\) 为整个坐标轴. 对于第 \(i\) 个任务区间 \([a ...
- 洛谷P1120 小木棍 [数据加强版](搜索)
洛谷P1120 小木棍 [数据加强版] 搜索+剪枝 [剪枝操作]:若某组拼接不成立,且此时 已拼接的长度为0 或 当前已拼接的长度与刚才枚举的长度之和为最终枚举的答案时,则可直接跳出循环.因为此时继续 ...
- react 中事件参数和一般参数以及this绑定的写法
import React from 'react'; import PropTypes from 'prop-types'; class Home extends React.Component{ r ...
- java web项目启动加载顺序
转载:https://www.cnblogs.com/writeLessDoMore/p/6935524.html web.xml加载过程(步骤): 1.启动WEB项目的时候,容器(如:T ...
- _exit和exit的区别
在linux的标准库函数中,有一套称作高级I/O的函数,我们熟知的printf .fopen .fread .fwrite都在此列,他们也被称作缓冲I/O.其特征是对应每一个打开的文件,都存在一个缓冲 ...
- MySQL-- 数据库的三范式
目前关系数据库有六种范式:第一范式(1NF).第二范式(2NF).第三范式(3NF).巴斯-科德范式(BCNF).第四范式(4NF)和第五范式(5NF,又称完美范式). 而通常我们用的最多的就是第一范 ...
- Normal Equation Algorithm求解多元线性回归的Octave仿真
Normal Equation算法及其简洁,仅需一步即可计算出theta的取值,实现如下: function [theta] = normalEqn(X, y) theta = zeros(size( ...
- 边界安全 - CDN/DMZ/网络协议
CDN 工具 - LuManager CDN DMZ 网络协议 - DNS Win7下搭建DNS服务器 - BIND 根域 顶级域(即相关国家域名管理机构的数据库,如中国的CNNIC) com n ...
- android handler 调用原理
1,调度原理 andriod提供了Handler 和 Looper 来满足线程间的通信.Handler先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(MessageExchange) ...