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. 对Can We Make Operating Systems Reliable and Secure 的翻译

    摘要:微内核-相对于大内核(monolithic kernels)来说,由于它的 lower performance,长期以来被认为是不可接受的.而现在,由于它潜 在的高可靠性(higher reli ...

  2. JavaScript -- 时光流逝(五):js中的 Date 对象的方法

    JavaScript -- 知识点回顾篇(五):js中的 Date 对象的方法 Date 对象: 用于处理日期和时间. 1. Date对象的方法 <script type="text/ ...

  3. python黑帽子

    1.TCP客户端 #AF_INET 使用标准的IPv4地址或者主机名 #SOCK_STREAM是一个客户端 import socket target_host = 'www.google.com' t ...

  4. Vue学习之路4-v-bind指令

    1. 定义 1.1 v-bind 指令被用来响应地更新 HTML 属性,其实它是支持一个单一 JavaScript 表达式 (v-for 除外). 2. 语法 2.1 完整语法:<span v- ...

  5. FCM算法的matlab程序(初步)

    FCM算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648430.html文章中已经介绍了FCM算法,现在用matlab程序实现它. 作者:凯鲁嘎 ...

  6. Glyphicons 字体图标

  7. 【English】20190306

    Delivery team交付团队consumption消费[kənˈsʌmpʃən] The Consulting Delivery team is focused on delivering va ...

  8. 【大数据技术】HBase与Solr系统架构设计

    如何在保证存储量的情况下,又能保证数据的检索速度. HBase提供了完善的海量数据存储机制,Solr.SolrCloud提供了一整套的数据检索方案. 使用HBase搭建结构数据存储云,用来存储海量数据 ...

  9. C#编程の模板

    C#泛型编程已经深入人心了.为什么又提出C#模板编程呢?因为C#泛型存在一些局限性,突破这些局限性,需要使用C#方式的模板编程.由于C#语法.编译器.IDE限制,C#模板编程没有C++模板编程使用方便 ...

  10. Linux系统下为何病毒少?原因竟是这个?

    Linux系统下为何病毒少?原因竟是这个? 可能不少人持这样一种观点,认为 Linux 病毒少是因为Linux不像Windows那么普及,其实这种观点很早已经被人批驳过了,一个最有力的论据是:如果写病 ...