Some suggestions of a C solution


  • 0
    C

    Re: Very intuitive solution accepted as best in C
    Your solution is very simple and straight forward and I have learnt a lot about how to add two strings and deal with the carry problem. However, I found their are two bugs in your code which may cause some serious problems.

    • in function addString, when we add two numbers, the sum's length would be no greater than the length of the longer one plus 1. Additionally, t, a char array that denotes the sum must end with '\0', so the sum's length should be plus 1 again to guarantee t would end correctly. Finally, when using malloc, the line should be char *t = (char*)malloc(sizeof(char)*((len1 > len2 ? len1 : len2) + 2)).
    • memory has to be freeed after being used. So making t a global variable and freeing it before return in isAdditiveNumber should fix this.

Log in to reply
 

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