leetcode---Multiply Strings---大整数乘法
来源:互联网 发布:sqlserver格式化工具 编辑:程序博客网 时间:2024/05/19 23:03
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.
class Solution {public: string multiply(string num1, string num2) { reverse(num1.begin(), num1.end()); reverse(num2.begin(), num2.end()); int l1 = num1.size(); int l2 = num2.size(); int ans[l1+l2]; memset(ans, 0, sizeof(ans)); for(int i=0; i<l1; i++) for(int j=0; j<l2; j++) ans[i+j] += (num1[i] - '0') * (num2[j] - '0'); int c = 0; int i = 0; while(i < l1 + l2 - 1 || c) { int s = ans[i] + c; ans[i] = s % 10; c = s / 10; i++; } string result = ""; i = l1 + l2 - 1; while(i && ans[i] == 0) //去掉前面的0 i--; if(i == 0 && ans[i] == 0) return "0"; while(i >= 0) { result = result + (char)(ans[i] + '0'); i--; } return result; }};
0 0
- leetcode---Multiply Strings---大整数乘法
- LeetCode | 43. Multiply Strings(大整数乘法)
- 大整数乘法--leetcode Multiply Strings
- Multiply Strings(大整数乘法)
- LeetCode-43. Multiply Strings (JAVA)大整数乘法
- LeetCode Multiply Strings(大整数相乘)
- leetcode-43. Multiply Strings(大整数相乘)
- LeetCode Multiply Strings 高精度乘法C++实现
- LeetCode Multiply Strings 高精度乘法C++实现
- LeetCode-Multiply Strings实现大数的乘法
- [leetcode] 43 Multiply Strings(模拟大数乘法)
- LeetCode 43. Multiply Strings(乘法)
- LeetCode 43 Multiply Strings(高精度乘法)
- 【大数乘法】Multiply Strings
- 大数乘法(Multiply Strings)
- Multiply Strings 大数乘法
- Multiply Strings:乘法运算
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
- 【Leetcode】Reverse String
- leetcode.91. Decode Ways
- Android 自定义view:实现ListView下拉的视差特效
- C++快速入门-标准输入输出
- hashCode and equals
- leetcode---Multiply Strings---大整数乘法
- java实现输出日历
- ${pageContext.request.contextPath}的作用
- Eclpise web开发时 添加web插件
- oracle instance_name db_name db_unique_name 与监听注册
- 检测是否超出int范围 && 11. Container With Most Water
- Silverlight如何加载SWF文件?
- Gym 100947I-What a Mess
- Activity的加载模式解读