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. 1 <= S.length <= 1000
  2. 1 <= N <= 10^9

代码:

class Solution {
public:
bool queryString(string S, int N) {
int num;
int len = S.length(); for(int i = 0; i <= N; i ++) {
string t = Binary(i);
if(S.find(t) == -1) return false;
}
return true;
}
string Binary(int x) {
string ans = "";
while(x) {
ans += x % 2 + '0';
x /= 2;
}
for(int i = 0; i < ans.size() / 2; i ++)
swap(ans[i], ans[ans.size() - i - 1]);
return ans;
}
};

#Leetcode# 1016. Binary String With Substrings Representing 1 To N的更多相关文章

  1. 【LeetCode】1023. Binary String With Substrings Representing 1 To N 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【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 ...

  3. [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 ...

  4. [LeetCode] Special Binary String 特殊的二进制字符串

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  5. 【LeetCode】761. Special Binary String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/special- ...

  6. leetcode 761. Special Binary String

    761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...

  7. [Swift]LeetCode761. 特殊的二进制序列 | Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  8. 761. Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  9. [LeetCode] Add Binary 二进制数相加

    Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...

随机推荐

  1. python 协程、I/O模型

    一.引子 (超哥协程) 并发本质:保存状态+切换 cpu正在运行一个任务,转而执行另一个任务的情概况:1.是该任务发生了阻塞:2.该任务计算的时间过长或有一个优先级更高的程序替代了它. 协程本质上就是 ...

  2. Mysql 索引 事物

    索引 针对庞大数据 加速查询 缺点 占用空间 分类: 普通索引: 通过 index 创建 唯一索引: 就是 unique key 主键索引: 就是 primary key 联合索引(多列)" ...

  3. 5.04-requests_cookies

    import requests # 请求数据url member_url = 'https://www.yaozh.com/member/' headers = { 'User-Agent': 'Mo ...

  4. 《JAVA程序设计》_第二周学习总结

    20175217吴一凡 一.IDEA的安装和使用 参考老师的教程Intellj IDEA 简易教程. 1.IDEA的安装 因为我已经习惯了在Linux上敲代码,所以我决定将IDEA安装在虚拟机上. 首 ...

  5. 【angularJs】阻止默认事件

    $scope.click = function($event){ $event.stopPropagation();//在函数体内加上这句代码就好} 作者:smile.轉角 QQ:493177502 ...

  6. hyperledger中文文档学习-4-构建第一个fabric网络

    接下来的操作都将在hyperledge环境安装构建的虚拟机的环境下进行 参考https://hyperledgercn.github.io/hyperledgerDocs/build_network_ ...

  7. apt-get update 更新 ubuntu时出现Hash sum mismatch的原因及解决方法

    $ sudo apt-get update ...... Hit http://mirrors.163.com trusty/main Sources                          ...

  8. AI numpy

    nan:not a number inf:infinate arange(start, stop, step):[start, stop),step是步长的数组 sin:正弦函数 cos:余弦函数

  9. 【Codeforces 212E】Restaurants

    Codeforces 212 E 题意:给一棵树,其中删去一个点,剩余的联通块们同一个联通块都得涂同一个颜色(黑或白),问黑色涂的个数有可能是哪些. 思路:肯定是背包. 假设现在删掉\(u\)这个节点 ...

  10. centos7 开放3306端口并可以远程访问

    开启远程访问: GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; 允许任何ip以roo ...