加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

#Leetcode# 1016. Binary String With Substrings Representing

发布时间:2020-12-14 04:34:00 所属栏目:大数据 来源:网络整理
导读: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

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;
    }
};

  五题打卡

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读