Easy and clear C++ solution


  • 0

    It's the same with https://leetcode.com/problems/add-binary/ this problem.

    class Solution {
    public:
        string addStrings(string num1, string num2) {
            reverse(num1.begin(),num1.end());
            reverse(num2.begin(),num2.end());
            int n=max(num1.size(),num2.size());
            int carry=0;
            string sum;
            for(int i=0;i<n;i++){
                if (i<num1.size()) carry+=num1[i]-'0';
                if (i<num2.size()) carry+=num2[i]-'0';
                sum.push_back(carry%10+'0');
                carry/=10;
            }
            if (carry) sum.push_back('1');
            reverse(sum.begin(),sum.end());
            return sum;
        }
    };
    
    

Log in to reply
 

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