LeetCode·Daily Question·415. String Addition·Simulation

Author: Xiao Xun
Link: https://leetcode.cn/problems/add-strings/solutions/2347085/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-fges/
Source: The copyright of LeetCode
belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

topic

 

train of thought

Title -> Given two strings, calculate their sum and return it as a string.

Enumerate directly from the tail to the head to simulate the actual calculation process. Every time a number is taken, the character is converted into a number, and then converted into a character after calculation for storage. Finally, it is judged whether there is a carry. If there is a carry, the carry is also storage.

A trick about the carry, because the AND operation, the carry can only have 1, so when the memory is opened, the carry position is opened up and assigned a value of 1, and when returning at the end, if there is a carry, the first address of the string is returned, and vice versa Return the first address of the string + 1 ---- to get the carry effect.

Code comments are super detailed

ps : I did this question once last year. When I looked back at the code at that time, I was filled with emotion. By looking at the code, I knew that the level of the code was very limited at that time, and the code was very redundant. After a year of study , the progress is still very obvious. Everyone work hard! ! !

 

the code

char * addStrings(char * num1, char * num2){
    int len1 = strlen(num1);
    int len2 = strlen(num2);
    int len = (len1 > len2 ? len1 : len2) + 2;
    char *ans = malloc(sizeof(char) * len);
    memset(ans, 0, len);//初始化
    ans[0] = '1';//进位赋值
    int c = 0;//记录进位
    for (int i = len - 2; i > 0; i--) {
        int sum = 0;//记录和
        if (len1 > 0) {//取数
            sum += (num1[len1 - 1] - '0');
            --len1;
        }
        if (len2 > 0) {//取数
            sum += (num2[len2 - 1] - '0');
            --len2;
        }
        sum += c;//加上一位的进位
        c = sum / 10;//保存进位
        sum %= 10;//保存当前位
        ans[i] = sum + '0';//字符存储
    }
    return c ? ans : ans+1;//判断进位返回对应地址
}

作者:小迅
链接:https://leetcode.cn/problems/add-strings/solutions/2347085/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-fges/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
char * addStrings(char * num1, char * num2){
    int num1_len = strlen(num1);
    int num2_len = strlen(num2);
    int len_1 = num1_len-1;
    int len_2 = num2_len-1;
    int max_len = num1_len>num2_len ? num1_len : num2_len;
    char * num = malloc(sizeof(char) * (max_len+2));
    int i =0,sum = 0,max = max_len+2;
    while(len_1>=0&&len_2>=0)
    {
        sum = (num1[len_1--]-'0')+(num2[len_2--]-'0')+i;
        i = sum/10;
        sum = sum%10;
        num[max_len--] = sum+'0';
    }
    while(len_1>=0)
    {
        sum = (num1[len_1--]-'0')+i;
        i = sum/10;
        sum = sum%10;
        num[max_len--] = sum+'0';
    }
    while(len_2>=0)
    {
        sum = (num2[len_2--]-'0')+i;
        i = sum/10;
        sum = sum%10;
        num[max_len--] = sum+'0';
    }
    if(i>0)
    {
        num[0] = i+'0';
        num[max-1] ='\0';
        return num;
    }
    num[max-1] ='\0';
    return ++num;
    
}


作者:小迅
链接:https://leetcode.cn/problems/add-strings/solutions/2347085/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-fges/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Guess you like

Origin blog.csdn.net/m0_64560763/article/details/131761702