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 解题报告的更多相关文章

  1. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  3. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

  4. LeetCode: Sort Colors 解题报告

    Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...

  5. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  6. 【LeetCode】1221. Split a String in Balanced Strings 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 统计 日期 题目地址:https://leetcode ...

  7. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  8. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  9. 【LeetCode】893. Groups of Special-Equivalent Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. openerp用wizard导入excel数据

    来自:http://blog.csdn.net/yumingbuzhongyao/article/details/18669183 作为一个quick note吧. OE里的csv导入数据功能形同摆设 ...

  2. Unity 文字爆炸(风化)消失效果 粒子系统应用

    利用Unity的粒子系统,使用C#代码控制粒子的位置和速度,实现文字风化爆炸的效果. Unity的东西,不像flash,不能直接放到网页中,没办法了,只能放截图了.有兴趣的可以下载看看:text_ex ...

  3. ribbon区域亲和配置一例

    只需在springboot的配置文件中添加以下内容即可: eureka.instance.metadata-map.zone=left 该配置是一个eureka客户端的配置,并且该客户端使用了ribb ...

  4. 通过Java发送邮件和接收邮件的工具类

    一.第一种 使用SMTP协议发送电子邮件 第一步:加入mail.jar包 (1)简单类型 package com.souvc.mail; import java.util.Date; import j ...

  5. 如何发布开源自己的框架或类库到CocoaPods - 图文讲解

    当自己的库已经上传GitHub后,那么如何快速简单的开源自己的库呢? 这里就是介绍如何将自己的类库上传到pods管理库,以便开源所有人都能方便使用. 准备前提: - 项目已上传到GitHub (注意, ...

  6. 解决LLDB模式下出现message sent to deallocated instance错误

    本文在源文的基础上做整理:http://www.devdiv.com/home.php?mod=space&uid=50901&do=blog&id=50856 Xcode版本 ...

  7. python matplotlib.pyplot画矩形图 以及plt.gca()

    plt的Rectangle参数: 第一个参数是坐标(x,y),即矩形的画图的起点坐标,这个起点坐标不是一味地从左下角开始画,而是对应整个图中坐标原点,即(0,0). 第二个参数是矩形宽度 第三个坐标是 ...

  8. VC++获取操作系统的版本 GetVersionEx函数

    原文链接: http://blog.sina.com.cn/s/blog_8a7012cf010189tn.html 函数:BOOL CSystemOperate::GetOSDisplayStrin ...

  9. 清理linux 某个文件夹下面所有的log文件

    #!/bin/sh #目标文件夹下面所有问题 target_dir="/app/" #删除2天前新建的后缀为log的文件 -name "*.log" -exec ...

  10. Verilog学习笔记

    作者:桂. 时间:2017-06-24  11:07:40 链接:http://www.cnblogs.com/xingshansi/p/7039237.html 前言 Verilog是硬件描述语言, ...