c++ solution


  • 0
    W
    class Solution {
    public:
    	string addStrings(string num1, string num2) {
    		int carry = 0;
    		string* shorter = NULL;
    		string* longer = NULL;
    		int len = 0;
    		if (num1.size() > num2.size()) {
    			shorter = &num2;
    			longer = &num1;
    			len = shorter->size();
    		}
    		else {
    			shorter = &num1;
    			longer = &num2;
    			len = shorter->size();
    		}
    		stringstream ss;
    		for (int i = 0; i < len; ++i) {
    			int n1 = shorter->operator[](shorter->size() - (i + 1)) - '0';
    			int n2 = longer->operator[](longer->size() - (i + 1)) - '0';
    			int s = n1 + n2 + carry;
    			carry = s / 10;
    			ss << (s % 10);
    		}
    		for (int i = len; i < longer->size(); ++i) {
    			int n2 = longer->operator[](longer->size() - (i + 1)) - '0';
    			int s = n2 + carry;
    			carry = s / 10;
    			ss << (s % 10);
    		}
    		if (carry) ss << 1;
    		string res = ss.str();
    		reverse(res.begin(), res.end());
    		return res;
    	}
    };
    

Log in to reply
 

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