[LeetCode]Multiply Strings
2015-08-08 23:25
274 查看
题目
Number: 43Difficulty: Medium
Tags: Math, String
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.
题解
输入两个字符串,输出一个字符串,为两个字符串的乘积。
模拟乘法。
代码
[code]string multiply(string num1, string num2) { string result(num1.size() + num2.size(), '0'); for(int i = num1.size() - 1; i >= 0; --i) { int carry = 0; for(int j = num2.size() - 1; j >= 0; --j) { int temp = (result[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0') + carry; result[i + j + 1] = temp % 10 + '0'; carry = temp / 10; } result[i] += carry; } size_t startpos = result.find_first_not_of("0"); if(string::npos != startpos) return result.substr(startpos); return "0"; }
相关文章推荐
- jquery代码阅读之jquery.type
- JavaStep --- 3. 读取输入
- [LeetCode]Combination Sum II
- HDU 5365(计算几何)
- 游戏交互设计中的114条原则
- JQuery中如何处理键盘事件
- 花瓶插花
- 对一个程序的思考6(指针与指针的指针)
- hdu 5366 The mook jong 动态规划(BC 50 C题)
- Android 内存泄露之------Thread
- LeetCode 18 4Sum K-sum系列
- 数组去重复
- spring-mvc-redis
- ACM中关于Output Limit Exceeded和Time Limit Exceeded
- 深入理解Aop编程思想
- 编写jQuery插件
- Hibernate学习之hibernate执行顺序
- CentOS6编译安装LAMP:编译安装 PHP5
- VC 多线程编程
- svn: E170000