c++ clear solution 3 ms


  • 0
    G

    string addStrings(string num1, string num2) {
    int icr = 0, idx = 0, len1 = num1.size(), len2 = num2.size();
    string s = "";
    while (idx < len1 || idx < len2)
    {
    char c1 = (idx < len1) ? num1[len1 - 1 - idx] : '0';
    char c2 = (idx < len2) ? num2[len2 - 1- idx] : '0';
    int n = (c1 - '0') + (c2 - '0') + icr;
    char c = (n > 9) ? ('0' + n - 10) : ('0' + n);
    icr = (n > 9) ? 1 : 0;
    s.append(1, c);
    idx++;
    }

    if (icr == 1) s.append("1");
    reverse(s.begin(), s.end());
        
        return s;
    }

Log in to reply
 

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