当前位置:网站首页>816. fuzzy coordinates

816. fuzzy coordinates

2022-06-26 03:46:00 Mr Gao

816. Fuzzy coordinates

We have some two-dimensional coordinates , Such as “(1, 3)” or “(2, 0.5)”, Then we remove all commas , Decimal point and space , Get a string S. Returns all possible raw strings to a list .

There will be no extra zeros in the original coordinate representation , So there won't be anything like "00", “0.0”, “0.00”, “1.0”, “001”, "00.01" Or some other smaller number for coordinates . Besides , There is at least one number before a decimal point , So it's not going to happen “.1” Numbers in form .

The last returned list can be in any order . And notice that between the two numbers returned ( After comma ) There's a space .

Example 1:
Input : “(123)”
Output : [“(1, 23)”, “(12, 3)”, “(1.2, 3)”, “(1, 2.3)”]

Example 2:
Input : “(00011)”
Output : [“(0.001, 1)”, “(0, 0.011)”]
explain :
0.0, 00, 0001 or 00.01 It's not allowed .

Example 3:
Input : “(0123)”
Output : [“(0, 123)”, “(0, 12.3)”, “(0, 1.23)”, “(0.1, 23)”, “(0.1, 2.3)”, “(0.12, 3)”]

Example 4:
Input : “(100)”
Output : [(10, 0)]
explain :
1.0 It's not allowed .

The solution code is as follows :

/** * Note: The returned array must be malloced, assume caller calls free(). */



 char *  f1(char *sh,int len,int i,int po){
    
     char *s=(char *)malloc(sizeof(char)*(len));
     s[0]='(';

     if(i==1){
    
         s[1]=sh[i];
         s[2]='\0';
         return s;
     }
     int j;
     int size=1;
     if(po>=1){
    
         for(j=1;j<po;j++){
    
             s[size++]=sh[j];
         }
         if(po!=i+1){
    
               s[size++]='.';

         }
         

       
         for(j=po;j<=i;j++){
    
             s[size++]=sh[j];

         }

     }
     s[size]='\0';

     if((sh[1]=='0'&&po>2)||(sh[i]=='0'&&po<=i)){
    
         s[0]='\0';
         return s;
     }
     return s;


 }


 char *  f2(char *sh,int len,int i,int po){
    
     char *s=(char *)malloc(sizeof(char)*(len+2));
     //printf("**%d %d",i,po);
     if(i==len-3){
    
         s[0]=sh[i+1];
       
            s[1]=')';

     s[2]='\0';
         return s;
     }
     int j;
     int size=0;
  // printf("dfsa");
     if(po>=1){
    
         for(j=i+1;j<po;j++){
    
             s[size++]=sh[j];
         }
         if(po<=len-2){
    
                s[size++]='.';
         }
        
         for(j=po;j<=len-2;j++){
    
             s[size++]=sh[j];

         }

     }
      s[size]=')';

     s[size+1]='\0';
     
     if(sh[i+1]=='0'&&po>i+2||(sh[len-2]=='0'&&po<=len-2)){
    
         s[0]='\0';
         return s;
     }
   
     return s;


 }

char ** ambiguousCoordinates(char * s, int* returnSize){
    
   
    int len=strlen(s);
    char **sp=(char **)malloc(sizeof(char *)*(len*len*len));
    int i;
    int size=0;
 
    for(i=0;i<len*len*len;i++){
    
 
        sp[i]=(char *)malloc(sizeof(char)*(len+6));
    }
    int j,k;
    for(i=1;i<=len-3;i++){
    
        int num=i;
        int num2=len-2-i;
        for(j=1;j<=num;j++){
    
            
            char* s1=f1(s,len,i,1+j);
            if(strlen(s1)==0){
    
                continue;
            }
          
            for(k=1;k<=num2;k++){
    
               
               
                 sp[size][0]='\0';
               
              
                   
              char*s2=f2(s,len,i,k+i+1);
               if(strlen(s2)==0){
    
                  continue;
              }
                
              strcat(sp[size],s1);
              int l1=strlen(s1);
             

              sp[size][l1]=',';
              sp[size][l1+1]=' ';
              sp[size][l1+2]='\0';
             strcat(sp[size],s2);

            // printf("**%s ",sp[size]);

             
               
              size++;


            }

        }
       

    }
   

 *returnSize=size;
        return sp;
   

}
原网站

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