字符串相乘
来源:互联网 发布:淘宝中年女服装长袖 编辑:程序博客网 时间:2024/06/05 11:49
给定两个代表数字的字符串,返回它们的乘积(字符串形式)
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.
Multiply Strings
遍历两个字符串,依次进位即可
先将vector中无效位的0去除然后逆序得到的string为所求
string multiply(string num1, string num2) { const int m = num1.size(); const int n = num2.size(); vector<int> a(m + n, 0); for (int i = 0; i < m; ++i) { int dig = 0; for (int j = 0; j < n; ++j) { a[i + j] += (num1[m - 1 - i] - '0')*(num2[n - 1 - j] - '0') + dig; dig = a[i + j] / 10; a[i + j] %= 10; } if (dig) a[n + i] += dig; } while (a.size() > 1 && a.back() == 0) a.pop_back(); string result; for (auto it = a.rbegin(); it != a.rend(); ++it) result += *it + '0'; return result;}
0 0
- 字符串相乘
- 字符串相乘
- 字符串相乘
- 面试:字符串: 字符串相乘
- 长字符串数字相乘
- 两个数字字符串相乘
- 两个字符串相乘
- 字符串表示 大数相乘
- 字符串相乘(算法)
- 大数相加、相乘 字符串处理
- 大数相乘代码-字符串实现
- [LeetCode] Multiply Strings 字符串相乘
- 字符串大数相加和相乘
- 长整数相乘的字符串表示法
- LeetCode | Multiply Strings(字符串相乘)
- leetcode 43 Multiply Strings (字符串相乘)
- leetcode【第六周】字符串数字相乘
- 大数字符串形式相加和相乘
- 【项目管理和构建】——Maven简介(一)
- 自定义AlertView
- JAVA 容器练习
- IntelliJ 使用心得
- https的一些特性
- 字符串相乘
- Java 类与继承
- TCP与UDP的区别
- swift 变量定义与使用+个人经验
- 应用Valgrind 发现 Linux 程序的内存问题
- IPtables中SNAT、DNAT和MASQUERADE的含义
- 蓝牙之十九-HFPclient
- ajax get请求与服务器建立连接
- 手把手教你收集产品情报