Always Runtime Error,I don't know why.


  • 0
    H
    char* addBinary(char* a, char* b) {
    char carry='0';
    char *c,*d;
    int len=0,len1=0,len2=0;
    int i;
    
    len1=strlen(a);
    len2=strlen(b);
    
    len=len1>len2?len1:len2;
    
    c=(char*)malloc(sizeof(char)*(len+1));
    memset(c,0,len+1);
    d=(char*)malloc(sizeof(char)*(len+2));
    memset(d,0,len+2);
    
    if(len1>len2)
    {
        b=(char*)realloc(b,sizeof(char)*len);
        
        for(i=len-1;i>=0;i--)
        {
            if(i>=(len-len2))
            {
                b[i]=b[i-(len-len2)];
            }
            else
            {
                b[i]='0';
            }
        }
    }
    else if(len1<len2)
    {
        a=(char*)realloc(a,sizeof(char)*len);
        
        for(i=len-1;i>=0;i--)
        {
            if(i>=(len-len1))
            {
                a[i]=a[i-(len-len1)];
            }
            else
            {
                a[i]='0';
            }
        }
    }
    
    for(i=len-1;i>=0;i--)
    {
        c[i]=a[i]+b[i]+carry-2*'0';
        if(c[i]=='2')
        {
            carry='1';
            c[i]='0';
        }
        else if(c[i]=='3')
        {
            carry='1';
            c[i]='1';
        }
        else
        {
            carry='0';
        }
    }
    
    if(carry=='1')
    {
        for(i=0;i<len+1;i++)
        {
            if(i==0)
            {
                d[0]='1';
            }
            else
            {
                d[i]=c[i-1];
            }
        }
        free(c);
        d[len+1]='\0';
        return d;
    }
    else
    {
        c[len]='\0';
        return c;`enter code here`
    }
    

    }


  • 0
    H

    I have solved it.I ignored that the test example should be array and the Memory space cannot be changed!

    The code shoule be:
    char* addBinary(char* a, char* b) {
    char carry='0';
    char *c,*d,*a1,*b1;
    int len=0,len1=0,len2=0;
    int i;
    
    len1=strlen(a);
    len2=strlen(b);
    
    len=len1>len2?len1:len2;
    
    a1=(char*)malloc(sizeof(char)*len);
    memset(a1,0,len);
    b1=(char*)malloc(sizeof(char)*len);
    memset(b1,0,len);
    c=(char*)malloc(sizeof(char)*(len+1));
    memset(c,0,len+1);
    d=(char*)malloc(sizeof(char)*(len+2));
    memset(d,0,len+2);
    for(i=0;i<len1;i++)
    {
        a1[i]=a[i];
    }
    for(i=0;i<len2;i++)
    {
        b1[i]=b[i];
    }
    
    if(len1>len2)
    {
        b1=(char*)realloc(b1,sizeof(char)*len);
        for(i=len-1;i>=0;i--)
        {
            if(i>=(len-len2))
            {
                b1[i]=b1[i-(len-len2)];
            }
            else
            {
                b1[i]='0';
            }
        }
    }
    else if(len1<len2)
    {
        a1=(char*)realloc(a1,sizeof(char)*len);
        
        for(i=len-1;i>=0;i--)
        {
            if(i>=(len-len1))
            {
                a1[i]=a1[i-(len-len1)];
            }
            else
            {
                a1[i]='0';
            }
        }
    }
    
    for(i=len-1;i>=0;i--)
    {
        c[i]=a1[i]+b1[i]+carry-2*'0';
        if(c[i]=='2')
        {
            carry='1';
            c[i]='0';
        }
        else if(c[i]=='3')
        {
            carry='1';
            c[i]='1';
        }
        else
        {
            carry='0';
        }
    }
    free(a1);
    free(b1);
    
    if(carry=='1')
    {
        for(i=0;i<len+1;i++)
        {
            if(i==0)
            {
                d[i]='1';
            }
            else
            {
                d[i]=c[i-1];
            }
        }
        free(c);
        d[len+1]='\0';
        return d;
    }
    else
    {
        c[len]='\0';
        return c;
    }
    

    }


  • 0
    L

    not memory space issues, but you may out of bound.

    This problem only need one memory zone, not need allocate two, even four.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.