1 题目

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.

接口: public String multiply(String num1, String num2);

2 思路

大整数的乘法,不能够简单用Integer.valueOf(String s),会产生溢出错误。

我们先来看一下289*758的计算过程:



首先我们把每一位相乘,得到一个没有进位的临时结果,如图中中间的一行红色数字就是临时结果(最多只有m + n - 1个,可以用数组保存),然后把临时结果从低位起依次进位。

对于一个m位整数乘以n位整数的结果,最后得到的结果最多是m+n位。

因此,得出下面的算法:

1.翻转string

2.建立中间结果数组,循环遍历两个string,把单位的乘积累加到数组相应的位置

3.处理进位并输出

4.注意前导零的测试用例和测试用例(0,0)

我们有趣的发现, 红色的中间结果是有规律的:

  • 109 = 88 + 95;这两对乘积所取的元素下标是(1,0)和(0,1),下标的和是1。
  • 110 = 28 + 85 + 9*7;这三对乘积所取的元素下标是(2,0) (1,1) (0,2),下标的和是2。

    利用这个特性,可以计算出中间结果数组。

复杂度: Time: O(n^2) ; Space: O(m + n)

3 代码

        public String multiply(String num1, String num2) {
StringBuilder s1 = new StringBuilder(num1).reverse();
StringBuilder s2 = new StringBuilder(num2).reverse(); final int len1 = s1.length();
final int len2 = s2.length();
int[] tmp = new int[len1 + len2 - 1];
for (int i = 0; i < len1; i++) {
for (int j = 0; j < len2; j++) {
tmp[i + j] += (s1.charAt(i) - '0') * (s2.charAt(j) - '0');
}
} StringBuilder result = new StringBuilder(len1 + len2);
for (int i = 0; i < tmp.length; i++) {
int mod = tmp[i] % 10;
int carry = tmp[i] / 10;
if (i + 1 < tmp.length) {
tmp[i + 1] += carry;
result.insert(0, mod);
} else {
result.insert(0, tmp[i]);
}
} while (result.charAt(0) == '0' && result.length() > 1) {
result.deleteCharAt(0);
} return result.toString();
}

4 总结

  • 想出利用中间的乘积结果来计算乘积是关键。
  • 如何求中间结果是有规律的。

5 扩展

更高效的计算大整数乘法一般有:

1.karatsuba算法,复杂度为3nlog3≈3n1.585,可以参考百度百科、面试题——大整数乘法、乘法算法-Karatsuba算法。

2.基于FFT(快速傅里叶变换)的算法,复杂度为o(nlogn), 可以参考FFT, 卷积, 多项式乘法, 大整数乘法

6 参考

leetcode面试准备:Multiply Strings的更多相关文章

  1. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  2. 【LeetCode练习题】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  3. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  4. 【一天一道LeetCode】#43. Multiply Strings

    一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...

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

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

  6. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  7. LeetCode OJ:Multiply Strings (字符串乘法)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  9. LeetCode题解之Multiply Strings

    1.题目描述 2.问题分析 按照手算乘法的过程进行计算,首先计算乘法,然后计算加法. 3.代码 string multiply(string num1, string num2) { string s ...

随机推荐

  1. 从源码角度深入理解Handler

    为了获得良好的用户体验,Android不允许开发者在UI线程中调用耗时操作,否则会报ANR异常,很多时候,比如我们要去网络请求数据,或者遍历本地文件夹都需要我们在新线程中来完成,新线程中不能更新UI, ...

  2. android开发之自定义AutoCompleteTextView

    AutoCompleteTextView,很多人都用过,有些情况下使用Google提供的ArrayAdapter作为适配器就可以完成需求,但是在实际开发中,我们经常需要开发自定义适配器来完成开发工作. ...

  3. CSS hack常用方案(摘选)

    邮箱因为默认了line-height?:170%,导致采用table元素时继承问题,可以采用line-height:50% 很好解决. 常 在使用float时,后面的显示不正常,因为继承了float了 ...

  4. [转载]js中__proto__和prototype的区别和关系

          首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性_ ...

  5. C 函数指针数组

    名字有点绕口,其实更应该翻译为指针函数数组. 记录下对Head-First C这一节的理解,几乎每天班车上都会咪两眼,几乎每次都是看不懂,敲一敲的时候才有些明白. 通俗点讲,这功能解决的是,具有同种签 ...

  6. 升级mac的java版本

    在OS X EI Capitan下, java版本太低,从oracle官网下载的dmg文件升级一直有问题, 我发现mac下的java环境有三处 #这应该是系统自带java环境,默认/usr/bin/j ...

  7. [转载]Access to the path '' is denied.解决方案

    原文地址:Access to the path '' is denied.解决方案作者:趴着墙等红杏 ccess to the path '路径' is denied.我在网上找了很多资料,最后终于解 ...

  8. 多个互相有联系的checkbox的单选逻辑

    checkbox单选的状态逻辑,状态好的时候一下就写好了,状态不好的时候要调试比较久,当然主要是对其中的事件不太清楚. 先给出效果图吧. 然后给出代码, selectZhiFuBaoPay.setOn ...

  9. 数据库性能高校:CPU使用过高(下)

    CPU使用率过高的常见原因 查询优化器会尽量从CPU,IO和内存资源成本最小的角度,找到最高效的数据访问方式.如果没有正确的索引,或者写的语句本身就会忽略索引, 又或者不准确的统计信息等情况下,查询计 ...

  10. ios 字符串替换方法

    string=[string stringByReplacingOccurrencesOfString:@"-"withString:@"/"];