Multiply Strings[LeetCode]
来源:互联网 发布:清华同方教学软件 编辑:程序博客网 时间:2024/06/11 07:35
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) {if(num1 == "0" || num2=="0") return "0";int len1 = num1.size();int len2 = num2.size();int flag = 0;int k = 0;string str(len1 + len2, '0');for(int i=len1-1;i>=0;i--){int a = num1[i] - '0';for(int j=len2-1, k=i+len2; j>=0; j--,k--){int b = num2[j] - '0';int unit = a*b %10;int cur_unit = str[k] - '0';str[k] = (cur_unit + unit + flag)%10 + '0';//flag为每次进位的值,待下次计算时加入flag = (a*b - unit)/10 + (cur_unit + unit + flag)/10;}//处理每次循环到num2的最高位后的flagint m = i;while(flag){int cur = str[m] - '0';str[m] = (cur + flag)%10 + '0';flag = (cur + flag)/10;m--;}}int x = 0;//去除str前面的‘0’ while (str[x] == '0') x++;str = str.substr(x,len1+len2-x);return str;}};
0 0
- LeetCode: Multiply Strings
- LeetCode Multiply Strings
- LeetCode: Multiply Strings
- [Leetcode] Multiply Strings
- [LeetCode] Multiply Strings
- [Leetcode] Multiply Strings
- LeetCode 45: Multiply Strings
- [LeetCode]Multiply Strings
- [LeetCode] Multiply Strings
- LeetCode-Multiply Strings
- [leetcode] Multiply Strings
- LeetCode - Multiply Strings
- Leetcode: Multiply Strings
- LeetCode题解:Multiply Strings
- leetcode Multiply Strings
- LeetCode Multiply Strings
- LeetCode | Multiply Strings
- [LeetCode] Multiply Strings
- C++第3周(春)项目3 时间类
- 黑马程序员-java集合笔记之map
- ORACLE FREELIST HWM(转)
- 关于VirtualBOX安装错误:VERR_MODULE_NOT_FOUND的解决方法。
- OpenGL学习记录4
- Multiply Strings[LeetCode]
- 使用着色器实现蒙皮动画和形变动画
- _存储一些有用的链接
- Autofac 基本例子
- <<J2SE>>Java代码自写9Path算法
- PL/SQL Developer+Oracle 连接远端数据库
- Nexus搭建私服
- 玩转html5<canvas>画图
- PowerPC从NorFlash启动的过程