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 ...
随机推荐
- 杀戮天使(Angels of Death)无删减版 高清下载
Created by Marydon on 已完结 免责声明 本人所提供的资源搜集于网络,仅供学习交流使用,不得进行任何商业及非法用途,由此产生的一切后果将由使用者本人承担: 本人仅仅提供一个观摩 ...
- EXCEPTION-STRUTS2
CreateTime--2016年8月29日17:05:50Author:Marydon 声明:异常类文章主要是记录了我遇到的异常信息及解决方案,解决方案大部分都是百度解决的,(这里只是针对我遇到 ...
- C#多线程之 ManualResetEvent和AutoResetEvent
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
- RHEL和Centos常用版本
学而优则仕,思则进步也Q Redhat: http://pan.baidu.com/s/1qXKRqqS Centos: http://pan.baidu.com/s/1o8RrjXw
- ASP.NET#使用母版时,如果要使用js中的getElementById()方法取得某个内容页的元素时要注意的问题
当使用母版,要使用js中的getElementById()方法取得某个内容页的元素时,所选取的id并不是母版中内容页的id,而是在设计内容页时设定的id例子:母版页: ...... <head ...
- windows批处理文件打印幻方
无论是批处理文件还是shell都是没有意义的,它们只是一种工具,并且是非常低级难懂的工具. 如果不会,那就保持不会就好了.不要花费太多时间在这些没意义的事情上. 批处理的没意义体现在: 难以表达 随便 ...
- jQuery:用 lightTreeview 实现树形分类菜单的功能 展开收缩分类代码
最近在做前端开发项目中,需要用到树形结构.在网上查阅到了许多相应资源.其中觉得lightTreeview是一款非常不错的JQ树形分类菜单代码,结构简单,支持多级.还有详细的参数可以配置,以实现各种效果 ...
- CoreData数据库升级
如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库的迁移,具体操作如下: 1.选中你的mydata.xc ...
- eclipse svn登陆用户保存信息删除
win7系统 eclipse svn saveinfo path:磁盘:\%用户名%\AppData\Roaming\Subversion\auth\svn.simple 例如:D:\Users\用户 ...
- ubuntu(14.04) 安装ssh,并使用root用户登录
1.apt-get install openssh-server 2.修改ssh的配置文件/etc/ssh/sshd_config 注释掉以前的:PermitRootLogin without-pas ...