leetcode---Multiply Strings---大整数乘法
2016-05-26 21:53
531 查看
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.
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; } };
相关文章推荐
- leetcode.91. Decode Ways
- Sublime 实用快捷键[Mac OS X]
- 动态规划---51nod 1055
- 个人最终总结
- 给定一个数字n,不用for循环实现输出数组 [1,2,3,4,...,n]
- Android 自定义view:实现ListView下拉的视差特效
- java的对象类型转换
- 【Leetcode】Reverse String
- 接口实现多态
- MFC程序中如何获取控件在客户区的坐标?
- codeforces 676C Vasya and String (尺取法)
- 用VC创建DEMO,调用动态库(文件界面)
- codeforces 414B (DP)
- 云栖社区
- 【Leetcode】Power of Four
- 配置hibernate根据实体类自动建表功能(转载)
- 在centos 上安装jenkins
- 解决不兼容getElementsByClassName
- php核心知识点
- 面向对象设计原则