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. exception PLS-00215: String length constraints must be in range (1 .. 32767)

      exception PLS-00215: String length constraints must be in range (1 .. 32767) CreationTime--2018年8月 ...

  2. ribbon区域亲和配置一例

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

  3. 使用loadrunner对https协议(单双向SSL)的web端性能测试 (转)

    1.项目背景 1.1 单双向SSL的含义及部署 单向SSL即我们说到的https协议. 特点是,浏览器需要请求验证服务器证书: 基本含义是:一个安全通信通道,它基于HTTP开发,用于在客户计算机和服务 ...

  4. Loadrunner脚本回放 场景运行过程中常见错误分析

    问题一:Loadrunner超时错误问题描述 Loadrunner超时错误:在录制Web协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 问题现象Error -27728: ...

  5. 【WebKit内核 CEF3 】 第一篇:下载分支代码并本地编译

    关于CEF  Chromium Embedded Framework 简单说就是  WebKit内核的 对外绑定. 当前主流浏览器内核 一.Trident内核代表产品Internet Explorer ...

  6. Android网络开发之Socket通信

    HTTP通信中Client发送的每次请求都需要Server回送响应,在请求结束后,Client会主动释放连接.从建立连接到隔壁连接的过程成为一次连接.要保持Client程序的在线状态,需要不断地向Se ...

  7. TScrollBox响应鼠标滚轮问题

    Delphi的TScrollBox本身并不响应鼠标滚轮事件(不知道为什么),但可以在ScrollBox的鼠标滚动事件中进行控制: procedure TfrmTaskNoteEdit.ScrollBo ...

  8. CSU1256 天朝的单行道(spfa)

    1256: 天朝的单行道 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 281  Solved: 92 [Submit][Status][pid=12 ...

  9. MySQL日期与时间戳互转函数

    -- 时间戳转日期 ); #日期转时间戳 Select UNIX_TIMESTAMP('2018-07-16 12:23:00');

  10. vue v-model 与 组件化的表单组件如何沟通

    参考mint-ui的代码: https://github.com/ElemeFE/mint-ui/blob/master/packages/radio/src/radio.vue https://gi ...