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. LeetCode算法题-Valid Palindrome(Java实现)

    这是悦乐书的第174次更新,第176篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第33题(顺位题号是125).给定一个字符串,确定它是否是回文,只考虑字母数字字符并忽略 ...

  2. Linux 基本操作--文件查看 (day3)

    一.查看文件-----cat (详情参考:http://blog.sina.com.cn/s/blog_52f6ead0010127xm.html) 语法结构: cat 查看方式 文件 cat  -A ...

  3. C# SHA256加密算法记录

    using System.Text; using System.Diagnostics; using System.Security; using System.Security.Cryptograp ...

  4. Java基础知识点(二)

    前言:Java的基础知识点不能间断. 1.Array和ArrayList的区别 关于Array的用法,参看:http://blog.csdn.net/b_11111/article/details/5 ...

  5. 「APIO2017」商旅

    「APIO2017」商旅 题目描述 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所深深吸引,决定定居于此,做一个商人.科巴有 \(N\) 个集市, ...

  6. vue自定义指令用法总结及案例

    1.vue中的指令有哪些?

  7. L2-010 排座位 (并查集)

    这里唯一需要注意的是,各个输出的条件在题目中有点描述模糊. 是朋友关系,(不管是不是间接朋友关系) 既不是朋友也不是敌人(这里不用管是不是间接朋友) 是敌人关系,同时是间接朋友关系 是单纯的敌人关系, ...

  8. SQLAlchemy中的自引用

    SQLALCHEMY采用adjacency list pattern来表示类的自引用. 例如,对于类Node自引用: class Node(Base): __tablename__='node' id ...

  9. face recognition[翻译][深度学习理解人脸]

    本文译自<Deep learning for understanding faces: Machines may be just as good, or better, than humans& ...

  10. FineUIPro v3.5.0发布了,减少 90% 的上行数据量,15行代码全搞定!

    一切为客户着想 一切的一切还得从和一位台湾客户的沟通说起: 客户提到将ViewState保存在服务器端以减少上行数据量,从而加快页面的回发速度. 但是在FineUI中,控件状态都保存在FState中, ...