Sunday, May 10, 2015

Multiply Strings

来源:Leetcode

原帖:https://oj.leetcode.com/problems/multiply-strings/

题目:
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative.
Solution: Just like what we do when multiplying integers.

代码:
 class Solution {  
 public:  
   string multiply(string num1, string num2) {  
     int N = num1.size(), M = num2.size();  
     string res(N + M, '0');  
     for (int i = N - 1; i >= 0; --i) {  
       int carry = 0;  
       for (int j = M - 1; j >= 0; --j) {  
         int sum = carry + (res[i + j + 1] - '0') +   
              (num1[i] - '0') * (num2[j] - '0');  
         res[i + j + 1] = sum % 10 + '0';  
         carry = sum / 10;  
       }  
       res[i] += carry; // add carry ; 不要忘记进位  
     }  
     while (res.size() > 1 && res[0] == '0') { // remove extra '0'  
       res.erase(res.begin());  
     }  
     return res;  
   }  
 };  

No comments:

Post a Comment