LeetCode: Multiply Strings 解题报告
Multiply Strings
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.
SOLUTION 1:
参考自http://blog.csdn.net/fightforyourdream/article/details/17370495
相当优雅的算法,主页君稍微改进,把翻转String这一步拿掉了。比起一般的做法,这个算法很容易就BUG FREE.
思路:
1 建立数组,双层循环遍历两个string,把单位的乘积累加到数组相应的位置
2 处理进位并输出
3 注意前导零的corner case
public class Solution {
public String multiply(String num1, String num2) {
if (num1 == null || num2 == null) {
return null;
} int len1 = num1.length();
int len2 = num2.length(); int[] product = new int[len1 + len2]; // 计算相应位置的product.
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
// 注意,这里要使用+=以不断累加乘积
product[i + j] += (num1.charAt(len1 - 1 - i) - '0') * (num2.charAt(len2 - 1 - j) - '0');
}
} StringBuilder ret = new StringBuilder(); int carry = 0;
// 计算进位
for (int i = 0; i < len1 + len2; i++) {
product[i] = product[i] + carry;
int digit = product[i] % 10;
carry = product[i] / 10;
ret.insert(0, digit);
} // 去掉前导0
while (ret.length() > 1 && ret.charAt(0) == '0') {
ret.deleteCharAt(0);
} return ret.toString();
}
}
2015.1.20 redo
public String multiply(String num1, String num2) {
// 18:24
if (num1 == null || num2 == null) {
return null;
} int len1 = num1.length();
int len2 = num2.length();
int[] sum = new int[len1 + len2]; // sum all of the mutiply result.
for (int i = ; i < len1; i++) {
for (int j = ; j < len2; j++) {
sum[i + j] += (num1.charAt(len1 - - i) - '') * (num2.charAt(len2 - - j) - '');
}
} int carry = ;
for (int i = ; i < len1 + len2; i++) {
sum[i] = sum[i] + carry; // Bug1: this line should be processed first.
carry = sum[i] / ;
sum[i] %= ;
} StringBuilder sb = new StringBuilder();
for (int i = ; i < len1 + len2; i++) {
sb.insert(, sum[i] + "");
} // delete the leading "0"
while (sb.charAt() == '' && sb.length() != ) {
sb.deleteCharAt();
} return sb.toString();
}
请至主页群GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/Multiply.java
LeetCode: Multiply Strings 解题报告的更多相关文章
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- LeetCode - Course Schedule 解题报告
以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...
- 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...
- 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...
- 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- ubuntu终止进程的方法
在ubuntu中,终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. 先看两个例子: 例子一:结束某个程序,如Firefox 键入命令: ...
- JSON path
https://github.com/itguang/gitbook-smile/blob/master/springboot-fastjson/fastjson%E4%B9%8BJSONPath%E ...
- JUC-Callable
实现线程的方式有四种: 1,实现runnable接口: 2,继承Thread. 3,也就是本节的Callable接口. 4,使用线程池. 区别: 实现Callable接口的方式,相较于实现Runnab ...
- glusterFS分布式存储部署流程
转自:http://bangbangba.blog.51cto.com/3180873/1712061 GlusterFS是一款非常易于使用的分布式文件存储系统,实现了全部标准POSIX接口,并用fu ...
- Loadrunner脚本编程(3)- 检查点,关联等函数
http://www.360doc.com/content/10/0806/13/1698198_44078093.shtml 1. 错误预防和恢复 参数默认是用{}括起来的,但也可以指定用< ...
- HUDOJ-----1394Minimum Inversion Number
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDUOJ -----1864 最大报销额(动态规划)
最大报销额 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 使用JavaStcript对数组元素去重的方法
在做javascript开发的时候,经常会遇到数组元素重复的问题,而javascript Array又没有直接提供方法解决此问题,还需要自己去实现. 方案一: 思路: 1.构建一个新的数组存放结果: ...
- 百度UEditor富文本编辑器去除过滤div等标签
将设计排版好的页面html代码上传到数据库,再读取出来的时候发现所有的div都被替换成了p标签. 解决方法: 首先在ueditor.all.js文件内搜索allowDivTransToP,找到如下的代 ...
- iOS开发 - 用AFNetworking实现https单向验证,双向验证
https相关 自苹果宣布2017年1月1日开始强制使用https以来,htpps慢慢成为大家讨论的对象之一,不是说此前https没有出现,只是这一决策让得开发者始料未及,博主在15年的时候就做过ht ...