# Leetcode 43. 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.
• Converting the input string to integer is NOT allowed.
• You should NOT use internal library such as BigInteger.

3  4  （是不是很多小朋友喜欢将大的整数作为被乘数的，呵呵）
*1  2
---------
6  8
3 4
---------
4 0  8

s1="12";

s2="34"

AC=3,

BD=8,

AC代码如下：

string multiply(string num1, string num2)
{
//考虑乘积为0的情况
if(num1 == "0" || num2 == "0")
return "0";
//第一步，将两个字符串转换为整型数组
int len1 = num1.length();
int len2 = num2.length();
int *n1 = new int[len1];
int *n2 = new int[len2];
for(int i = 0 ;i < len1;i++)
n1[i] = num1[i] - '0';
for(int i = 0;i < len2;i++)
n2[i] = num2[i] - '0';
//新建一个res整型数组存放中间结果
int *res = new int[len1 + len2];
memset(res,0,sizeof(int)*(len1+len2));
//重点部分
for(int i = 0;i < len1;i++)
for(int j = 0;j < len2;j++)
res[i + j] += n1[i] * n2[j];
//res[i] 只能有一位，多的要进位，因此将进位处理后的结果放入temp整型数组中
//temp中每一位只能有一个数字，其中temp[0]可能有2位，因此要最后通过carry判断有无进位
int carry = 0;
int *temp = new int[len1 + len2];
memset(temp,0,sizeof(int)*(len1+len2));
for(int i = len1 + len2 -2;i >= 0;i--)
{
temp[i] = res[i] + carry;
carry = temp[i] / 10;
temp[i] = temp[i] % 10;

}
//carry 最后大于0表示temp有进位
string ans ="";
if(carry > 0 )
ans = ans+ char(carry + '0');
for(int k = 0;k <= len1 + len2 -2;k++)
ans = ans + char(temp[k] + '0');
return ans;
}


#### LeetCode（43）Multiply Strings

2015-08-28 17:42:02

#### Leetcode #43. Multiply Strings 字符串相乘 解题报告

2016-04-14 23:57:04

#### LeetCode --- 43. Multiply Strings

2015-02-10 08:57:25

#### 43. Multiply Strings leetcode python 2016 new season

2016-01-25 04:46:28

#### [LeetCode-Java]43. Multiply Strings

2016-10-08 20:34:31

#### [leetcode-43]Multiply Strings(java)

2015-07-30 21:37:51

#### [Leetcode]43. Multiply Strings @python

2016-01-17 13:12:47

#### leetcode -- Multiply Strings -- python容易处理，需要看其他解法

2015-12-10 18:18:01

#### [LeetCode][Java] Multiply Strings

2015-07-13 17:45:32

#### 43. Multiply Strings（大数相乘）

2016-03-03 22:18:11