标签:
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.
class Solution { public: string multiply(string num1, string num2) { int len1=num1.length(),len2=num2.length(); string res(len1+len2,‘0‘); for(int i=len1-1;0<=i;i--){ int carry=0; for(int j=len2-1;0<=j;j--){ int tmp=(res[i+j+1]-‘0‘)+(num1[i]-‘0‘)*(num2[j]-‘0‘)+carry; res[i+j+1]=tmp%10+‘0‘;//各位处理 carry=tmp/10; } res[i]+=carry;//最高位处理 } size_t startpos = res.find_first_not_of("0"); if (string::npos != startpos) { return res.substr(startpos); } return "0"; } };
标签:
原文地址:http://www.cnblogs.com/wqkant/p/5379071.html