题目链接

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

大整数乘法


我们以289*785为例

首先我们把每一位相乘,得到一个没有进位的临时结果,如图中中间的一行红色数字就是临时结果,然后把临时结果从低位起依次进位。对于一个m位整数乘以n位整数的结果,最多只有m+n位。         本文地址

注意:结果中需要去掉前导0,还需要注意结果为0的情况

class Solution {
public:
string multiply(string num1, string num2) {
int n1 = num1.size(), n2 = num2.size();
vector<int> tmpres(n1+n2, 0);
int k = n1 + n2 - 2;
for(int i = 0; i < n1; i++)
for(int j = 0; j < n2; j++)
tmpres[k-i-j] += (num1[i]-'0')*(num2[j]-'0');
int carryBit = 0;
for(int i = 0; i < n1+n2; i++)//处理进位
{
tmpres[i] += carryBit;
carryBit = tmpres[i] / 10;
tmpres[i] %= 10;
}
int i = k+1;
while(tmpres[i] == 0)i--;//去掉乘积的前导0
if(i < 0)return "0"; //注意乘积为0的特殊情况
string res;
for(; i >= 0; i--)
res.push_back(tmpres[i] + '0');
return res;
}
};

上述算法的复杂度为O(n^2)(假设整数长度为n)

另外更高效的计算大整数乘法一般有:(1)karatsuba算法,复杂度为3nlog3≈3n1.585,可以参考百度百科面试题——大整数乘法乘法算法-Karatsuba算法。(2)基于FFT(快速傅里叶变换)的算法,复杂度为o(nlogn), 可以参考FFT, 卷积, 多项式乘法, 大整数乘法

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3735309.html

LeetCode:Multiply Strings的更多相关文章

  1. LeetCode: Multiply Strings 解题报告

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

  2. [LeetCode] Multiply Strings 字符串相乘

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

  3. [leetcode]Multiply Strings @ Python

    原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...

  4. LeetCode: Multiply Strings. Java

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

  5. [Leetcode] Multiply strings 字符串对应数字相乘

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

  6. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

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

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

  8. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  9. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

随机推荐

  1. HTML字符实体(关于&nbsp;&gt;&lt;等)

    常用字符实体 注意:实体名称是区分大小写的! 结果 描述 实体名 实体编号    non-breaking space     < less than < < > greate ...

  2. 【JavaScript】获取未知类的结构

    目录结构: // contents structure [-] 为什么需要获取类的结构 关于JavaScript中的类 定义类的方法 第一种 第二种 DEMO HTML页面 date文件 注意事项 参 ...

  3. webpack初入

    序言:前面已经倒腾了grunt.gulp.fis3,今天来通过一个例子玩玩webpack!最终预览 通过下面的例子,能够了解以下几点: 1.如何安装webpack 2.如何使用webpack 3.如何 ...

  4. Linux0.11内核--加载可执行二进制文件之2.change_ldt

    前面分析完了copy_strings函数,这里来分析另一个注意的函数change_ldt. 先来看调用处: // 根据a_text 修改局部表中描述符基址和段限长,并将参数和环境空间页面放置在数据段末 ...

  5. 隐式启动判断是否有匹配的Intent

    一.PackageManager的resolveActivity public abstract ResolveInfo resolveActivity(Intent intent, int flag ...

  6. Android 四大组件之再论BroadCast

    BroadCast 是android提供的跨进程通讯的有一利器. 1.异步执行onReceiver @Nullable public abstract Intent registerReceiver( ...

  7. Android 最全Activity生命周期

    新进入Activity:onCreate > onStart > onResume 退出Activity:onPause > onStop > onDestroy 目前处于该A ...

  8. C#实现在图片上斜着写字

    最近公司要搞微信活动页面,要实现图片上可以写自己名字的功能,于是就查了一下怎么实现,下面贴一下代码备忘,希望大家也能用到: 我是在控制台应用程序里进行试验的. using (Image bitmap ...

  9. c#中 命令copy 已退出,返回值为1

    c#中重新生成时,报错:命令"copy ...... " 已退出,返回值为1.   错误截图如下: 解决办法: 点击"项目"右键--"属性" ...

  10. Python基础之面向对象

    一.面向对象概述 面向对象编程——Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程 ...