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.
Note:
- The length of both num1 and num2 is < 110.
- Both num1 and num2 contain only digits 0-9.
- Both num1 and num2 do not contain any leading zero, except the number 0 itself.
- You must not use any built-in BigInteger library or convert the inputs to integer directly.
思路:
(Ps: 手写稍微忍耐一些吧 ^_^)
我们发现: p[i+j] = a[i] * b[j] + 进位;
代码
1 | char* multiply(char* num1, char* num2) |
运行1W次时间:1
2
3
4
5char * str1 = "9999999999999999999999999999999999999999999999999999";
char * str2 = "9999999999999999999999999999999999999999999999999999";
int i = 0;
for( i = 0 ; i <10000 ; i++)
multiply(str1, str2);
1 | real 0m0.455s |
LeetCode 测试结果: