[Daily question] 415. String addition

[Daily question] 415. String addition

415. String addition

topic description

Given two non-negative integers num1 and num2 in string form, compute their sum and return it as a string.

You cannot use any of the built-in libraries for handling large integers (such as BigInteger), nor can you directly convert input strings to integer form.

Example 1:

输入:num1 = "11", num2 = "123"
输出:"134"

Example 2:

输入:num1 = "456", num2 = "77"
输出:"533"

Example 3:

输入:num1 = "0", num2 = "0"
输出:"0"

hint:

1 <= num1.length, num2.length <= 104
Both num1 and num2 contain only digits 0-9
Neither num1 nor num2 contain any leading zeros

problem solving ideas

Idea 1: Traverse the strings num1 and num2 to get the corresponding integers n1 and n2, and then add n1 and n2 and convert them into strings. However, due to the large data range, this method is not feasible. (not feasible)

class Solution {
public:
    string addStrings(string num1, string num2) {
        long n1=0,n2=0;
        for(int i=0;i<num1.size();i++)
        {
            n1=n1*10+(num1[i]-'0');
        }
        for(int i=0;i<num2.size();i++)
        {
            n2=n2*10+(num2[i]-'0');
        }
        long res=n1+n2;
        return to_string(res);
    }
};

Idea 2: Vertical subtraction. Use sum, cur, and add to represent the current bit sum, current bit, and carry respectively. First traverse the common part of num1 and num2, then traverse the remaining part of num1 or num2, and then process add. The idea of ​​this code is relatively clear, but the code is not concise. (feasible)

class Solution {
public:
    string addStrings(string num1, string num2) {
        int cur=0,sum=0,add=0;
        string res;
        //反转求和
        reverse(num1.begin(),num1.end());
        reverse(num2.begin(),num2.end());
        int i;
        //共同部分
        for(i=0;i<num1.size()&&i<num2.size();i++)
        {
            sum=(num1[i]-'0')+(num2[i]-'0')+add;
            cur=sum%10;
            add=sum/10;
            res.push_back(cur+'0');
        }
        //num1多余部分
        for(;i<num1.size();i++)
        {
            sum=(num1[i]-'0')+add;
            cur=sum%10;
            add=sum/10;
            res.push_back(cur+'0');
        }
        //num2多余部分
        for(;i<num2.size();i++)
        {
            sum=(num2[i]-'0')+add;
            cur=sum%10;
            add=sum/10;
            res.push_back(cur+'0');
        }
        //最后还剩下add
        if(add)
            res.push_back(add+'0');
        reverse(res.begin(),res.end());
        return res;
    }
};

Idea 3: vertical addition, the code is more concise. (feasible)

class Solution {
public:
    string addStrings(string num1, string num2) {
        int sum=0,add=0;
        string res;
        int i=num1.size()-1,j=num2.size()-1;
        int x,y; 
        //逆序遍历 进入循环的条件 字符串1有数据 或者字符串2有数据 或者进位不为0 三合一
        while(i>=0||j>=0||add!=0)
        {
            //特判字符串1
            x=i>=0?num1[i]-'0':0;
            //特判字符串2
            y=j>=0?num2[j]-'0':0;
            sum=x+y+add;
            res.push_back(sum%10+'0');
            add=sum/10;
            i--;
            j--;
        }
        //翻转字符
        reverse(res.begin(),res.end());
        return res;
    }
};

Summary: Pay attention to the data range! !

Guess you like

Origin blog.csdn.net/qq_43779149/article/details/131761269