判断从n到n/2即可,小于n/2都包含在其中。
暴力匹配即可
/*
* @lc app=leetcode id=1016 lang=cpp
*
* [1016] Binary String With Substrings Representing 1 To N
*/
// @lc code=start
class Solution {
public:
bool match(string& s, int loc, int k){
int N = s.length();
for(int i=loc;i>=0;i--){
if( (s[i]-'0') != (k&1) ){
return false;
}
k = k>>1;
if(k==0){
return true;
}
}
return false;
}
bool contains(string& s, int k) {
int N = s.length();
for(int i=N-1;i>=0;i--){
if(match(s, i, k)){
return true;
}
}
return false;
}
bool queryString(string s, int n) {
int N = s.length();
if(n > N*2) return false;
for(int i=n;i>n/2;i--){
if(contains(s, i) == false){
return false;
}
}
return true;
}
};
// @lc code=end