Simple C++ code


  • 0
    G
        string addStrings(string num1, string num2) {
            string ans;
            int c = 0;
            int i = num1.size() - 1, j = num2.size() - 1;
            while (i >= 0 || j >= 0 || c > 0) {
                int s = ((i >= 0) ? (num1[i--] - '0') : 0) + ((j >= 0) ? (num2[j--] - '0') : 0) + c;
                c = s / 10;
                s %= 10;
                ans.insert(0, 1, char(s + '0'));
            }
            return ans;
        }
    

Log in to reply
 

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