Multiply Strings Solutions in C++
Number 43
Difficulty Medium
Acceptance 33.9%
Link LeetCode
Other languages —
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/multiply-strings/// Author : Hao Chen// Date : 2014-07-18class Solution {public:string strPlus(string& num1, string& num2) {if (num1.size()==0) return num2;if (num2.size()==0) return num1;if ( num1.size() < num2.size() ) {swap(num1, num2);}string s;int carry=0;int x;for (int i=num1.size()-1, j=num2.size()-1; i>=0; i--, j--) {x = num1[i]-'0' + carry;if(j>=0){x += num2[j]-'0';}s.insert(s.begin(), x%10+'0');carry = x/10;}if (carry>0) {s.insert(s.begin(), carry+'0');}return s;}string multiply(string num1, string num2) {if (num1.size()<=0 || num2.size()<=0) return "";string result;for ( int i=num1.size()-1; i>=0; i--) {int carry = 0;string val;for( int j=num2.size()-1; j>=0; j--) {int v = (num2[j]-'0') * (num1[i]-'0') + carry;val.insert(val.begin(), v%10+'0');carry = v/10;}if (carry) val.insert(val.begin(), carry+'0');for (unsigned int j=i; j<num1.size()-1; j++) {val.push_back('0');}result = strPlus(result, val);}//check if it is zeroif (result[0]=='0') return "0";return result;}};int main(int argc, char**argv){string s1="20";string s2="25";if (argc>2){s1 = argv[1];s2 = argv[2];}cout << s1 << " * " << s2 << " = " << multiply(s1, s2) << endl;return 0;}
C++ solution by pezy/LeetCode
#include <string>using std::string;class Solution {public:string multiply(string num1, string num2) {string ret(num1.size() + num2.size(), '0');for (int i = num1.size()-1; i >= 0; --i) {int carry = 0;for (int j = num2.size()-1; j >= 0; --j) {int sum = (ret[i+j+1]-'0') + (num1[i]-'0') * (num2[j]-'0') + carry;ret[i+j+1] = sum%10 + '0';carry = sum/10;}ret[i] += carry;}size_t pos = ret.find_first_not_of('0');if (pos != string::npos) return ret.substr(pos);else return "0";}};