addString

/*************************************************************************************** 
 *
 * Given two non-negative numbers num1 and num2 represented as string, return the sum 
 * of num1 and num2.
 * 
 * Note:
 * 
 * The length of both num1 and num2 is 
 * Both num1 and num2 contains only digits 0-9.
 * Both num1 and num2 does not contain any leading zero.
 * You must not use any built-in BigInteger library or convert the inputs to integer 
 * directly.
 ***************************************************************************************/
/*************************************************************************************** 
 從右至左依次取字符串中的字符進行加減,注意最後的進位
 ***************************************************************************************/


class Solution {
public:
    string addStrings(string num1, string num2) {
        string& longstr  = ( num1.size() >= num2.size() ? num1 : num2 );
        string& shortstr = ( num1.size() <  num2.size() ? num1 : num2 );
        
        int longlen = longstr.size();
        int shortlen = shortstr.size();
        
        char carry = 0;
        int i, j;
        
        string result;
        for (i = longlen-1, j=shortlen-1; i>=0; i--, j--) {
            int add = 0;
            if (j>=0) {
                //char字符變量利用其在ASCII裏的對應值進行加減運算
                add = longstr[i] + shortstr[j] - 2 * '0' + carry; 
            }else{
                add = longstr[i] - '0' + carry;
            }
            carry = add/10;
            result = char('0' + add % 10) + result;
        }
        
        if (carry) {
            result = '1' + result;
        }
        return result;
    }
};

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章