当前位置:网站首页>1016. substring can represent binary string of numbers from 1 to n

1016. substring can represent binary string of numbers from 1 to n

2022-06-21 23:13:00 Mr Gao

1016. A substring can represent from 1 To N A binary string of numbers

Given a binary string s And a positive integer n, If for [1, n] Every integer in the range , Its binary representation is s Of Substring , Just go back to true, Otherwise return to false .

Substring Is a sequence of consecutive characters in a string .

Example 1:

Input :s = “0110”, n = 3
Output :true

Example 2:

Input :s = “0110”, n = 4
Output :false

int len;
char *f(int n){
    
 
 len=0;
   int p=n;
    while(p){
    
  len++;
        p=p/2;
     
    }
      
       char *cs=(char *)malloc(sizeof(char)*(len+1));
       int i=0;
       p=n;
        while(p){
    
     // printf(" %d ",len-i-1);
        cs[len-i-1]='0'+p%2;
        i++;
        p=p/2;

        }
       
      cs[len]='\0';
    // printf("%s ",cs);
      return cs;
}
bool f2(char *s, char *cs,int len){
    
    int i;
    for(i=0;i<len;i++){
    
        if(s[i]!=cs[i]){
    
            return false;
        }
    }
    return true;

}

bool queryString(char * s, int n){
    

int i;
int j;
for(j=1;j<=n;j++){
    

   int r=0;

    char *cs=f(j);
    int sl=strlen(s);
    for(i=0;i<=sl-len;i++){
    
        if(f2(cs,s+i,len)){
    
            r=1;
            break;
        }

    }
    if(r!=1){
    
        return false;
    }
}

return true;;
}
原网站

版权声明
本文为[Mr Gao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212052491152.html