C answer always has runtime error on Leetcode but workable on Mac with gcc and Xcode.


  • 0
    D

    There is always a runtime error comes out. I can successfully run this code on my Mac with gcc and also Xcode. Will appreciate someone point out what's wrong with my code.

      void reverseStr(char *str) 
    {
       int len = 0;
       int init = 0;
    
       while (*(str + len) != '\0') {
            len++;
       }
    
       len--;
    
       while (init < len) {
         char temp = *(str + init);
         *(str + init) = *(str + len);
         *(str + len) = temp;
         init++;
         len--;
       }
    }
    
    
    
    char*
     addBinary (char *a, char *b)
     {
         // check maximum length
         size_t aLength = strlen(a);
         size_t bLength = strlen(b);
         size_t maxLength = (aLength > bLength) ? aLength : bLength;
    
         char *c = malloc(sizeof(char) * (maxLength + 2));
         strcpy(c, ((aLength > bLength) ? a : b));
    
         int counter = 0;
         int temp = 0;
         char *ptrA = malloc(sizeof(char) * (maxLength));
         strcpy(ptrA, a);
         char *ptrB = malloc(sizeof(char) * (maxLength));
         strcpy(ptrB, b);
    
         //reverse a string and b string
         reverseStr(ptrB);
         reverseStr(ptrA);
    
         while (counter <= maxLength) {
             char digitA = *(ptrA + counter);
             char digitB = *(ptrB + counter);
             int valueA = (digitA == '1') ? 1 : 0;
             int valueB = (digitB == '1') ? 1 : 0;
             int sum = valueA + valueB + temp;
    
             if ((maxLength == counter) && sum == 0) break;
    
             if (sum % 2 == 1 && sum/2 == 0) {
                 temp = 0;
                 *(c + counter) = '1';
             }
             else if (sum % 2 == 0 && sum/2 == 1) {
                 temp = 1;
                 *(c + counter) = '0';
             }
             else if (sum % 2 == 1 && sum/2 == 1) {
                 temp = 1;
                 *(c + counter) = '1';
             }
             else {
                 temp = 0;
                 *(c + counter) = '0';
             }
    
             counter++;
         }
    
         //reverse back
         reverseStr(c);
         free(ptrA);
         free(ptrB);
    
         return c;
     }		/* -----  end of function addBinary  ----- */

  • 0
    D

    Problem solved with calloc function. I changed malloc to calloc and the result is accepted.

       void reverseStr(char *str) 
    {
      int len = 0;
      int init = 0;
    
      while (*(str + len) != '\0') {
            len++;
      }
    
      len--;
    
      while (init < len) {
         char temp = *(str + init);
         *(str + init) = *(str + len);
         *(str + len) = temp;
         init++;
         len--;
      }
    }
    
    char*
     addBinary (char *a, char *b)
     {
         // check maximum length
         size_t aLength = strlen(a);
         size_t bLength = strlen(b);
         size_t maxLength = (aLength > bLength) ? aLength : bLength;
    
         char *c = calloc((maxLength + 2), sizeof(char));
         strcpy(c, ((aLength > bLength) ? a : b));
    
         int counter = 0;
         int temp = 0;
         char *ptrA = calloc((maxLength + 1), sizeof(char));
         strcpy(ptrA, a);
         char *ptrB = calloc((maxLength + 1), sizeof(char));
         strcpy(ptrB, b);
    
         //reverse a string and b string
         reverseStr(ptrB);
         reverseStr(ptrA);
    
         while (counter <= maxLength) {
             char digitA = *(ptrA + counter);
             char digitB = *(ptrB + counter);
             int valueA = (digitA == '1') ? 1 : 0;
             int valueB = (digitB == '1') ? 1 : 0;
             int sum = valueA + valueB + temp;
    
             if ((maxLength == counter) && sum == 0) break;
    
             if (sum % 2 == 1 && sum/2 == 0) {
                 temp = 0;
                 *(c + counter) = '1';
             }
             else if (sum % 2 == 0 && sum/2 == 1) {
                 temp = 1;
                 *(c + counter) = '0';
             }
             else if (sum % 2 == 1 && sum/2 == 1) {
                 temp = 1;
                 *(c + counter) = '1';
             }
             else {
                 temp = 0;
                 *(c + counter) = '0';
             }
    
             counter++;
         }
    
         //reverse back
         reverseStr(c);
         free(ptrA);
         free(ptrB);
    
         return c;
     }		/* -----  end of function addBinary  ----- */

Log in to reply
 

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