Easy c++ solution during contest


  • 0
    S
    class Solution {
    public:
        string addStrings(string num1, string num2) {
            int i=0,  j,  sz1, sz2, carry = 0;
            sz1 = num1.size() - 1;
            sz2 = num2.size() - 1;
            string result;
            while( sz1>=0  &&  sz2>=0 ){
                int t = (num1[sz1--] -'0') + (num2[sz2--] - '0') + carry;
                result += (t % 10) + '0';
                carry = t/10;
            }
            while(sz1>=0){
                int t = (num1[sz1--] - '0') + carry;
                result += (t % 10) + '0';
                carry = t/10;
            }
            while(sz2>=0){
                int t = (num2[sz2--] - '0') + carry;
                result += (t % 10) + '0';
                carry = t/10;
            }
            if(carry) result += carry + '0';
            reverse(result.begin(),result.end());
            return result;
        }
    };
    

Log in to reply
 

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