Simple C++


  • 0
    F
    class Solution {
    public:
        string addStrings(string num1, string num2) {
            int i = num1.length()-1, j = num2.length()-1;
            int cary = 0;
            int curr1, curr2;
            string out;
            while(i >= 0 || j >= 0){
                curr1 = 0;
                curr2 = 0;
                if(i >= 0) curr1 = num1[i]-'0';
                if(j >= 0) curr2 = num2[j]-'0';
                curr1 = curr1 + curr2 + cary;
                out = out + (char)((curr1%10) + '0');
                cary = 0;
                if(curr1 > 9) cary = 1;
                --i;
                --j;
            }
            if(cary > 0) out = out + '1';
            reverse(out.begin(), out.end());
            return out;
        }
    };
    
    

Log in to reply
 

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