43. Multiply Strings
来源:互联网 发布:linux操作系统原理 编辑:程序博客网 时间:2024/06/05 02:39
题目要求:
Given two non-negative integers num1
and num2
represented as strings, return the product of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 110. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
代码如下:
string multiply(string num1, string num2) { int m = num1.size(), n = num2.size(); if(m == 0 || n == 0) return 0; string res(m + n, '0'); reverse(num1.begin(), num1.end()); reverse(num2.begin(), num2.end()); for(int i = 0; i < m; ++i) { int r = 0; for(int j = 0; j < n; ++j) { int tmp = (res[i + j] - '0') + (num1[i] - '0') * (num2[j] - '0') + r; res[i + j] = tmp % 10 + '0'; r = tmp / 10; } res[i + n] += r; } reverse(res.begin(), res.end()); size_t pos = res.find_first_not_of("0"); if(pos != string::npos) { return res.substr(pos); } return "0"; }
0 0
- [LeetCode]43.Multiply Strings
- LeetCode --- 43. Multiply Strings
- [Leetcode] 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- [leetcode] 43.Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings LeetCode
- 43. Multiply Strings
- 43. Multiply Strings
- 43. Multiply Strings
- leetcode 43. Multiply Strings
- LeetCode 43. Multiply Strings
- LeetCode 43. Multiply Strings
- LeetCode 43. Multiply Strings
- 原码、反码、补码以及为什么要用反码和补码
- kubernetes1.6新特性:支持新的卷插件
- Linux中进程描述符tast_struct结构体详解
- Android Activity之生命周期
- servlet
- 43. Multiply Strings
- Leetcode 257. Binary Tree Paths
- 如何快速学会Git Bash
- GIT篇之如何删除本地仓库?
- Java中等待提交线程池的所有线程执行完成
- spring中路径问题
- winsok编程历程
- WAVE文件格式
- Fragment中传入数据