题目链接

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. 断言与异常(Assertion Vs Exception)

    在日常编程实践中,断言与异常的界限不是很明显,这也使得它们常常没有被正确的使用.我也在不断的与这个模糊的怪兽搏斗,仅写此文和大家分享一下我的个人看法.我想我们还可以从很多角度来区别断言和异常的使用场景 ...

  2. React-Native性能优化点

    shouldComponentUpdate 确保组件在渲染之后不需要再更新的,即静态组件,尽量在其中增加shouldComponentUpdate方法,防止二次消耗所产生的性能消耗 shouldCom ...

  3. Linux vi/vim(转载)

    Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主 ...

  4. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

  5. 隐藏tabbar的属性hidesBottomBarWhenPushed

    项目中有需求是A视图控制器push之后B视图控制器需要隐藏底部的tabbar,在pop之后A视图控制器仍然显示tabbar. 其实不需要在push操作时敲 self.hidesBottomBarWhe ...

  6. html中select标签根据枚举获得值的总结

    不知不觉在公司一个多月了,这一个月做了一个支票申请的web页面功能,都不是特别难,审核有公司给的工作流,分页工具和很多公用工具公司也都给了,所以觉得难度都不是很大.今天主管让我们修改了以前做的项目的代 ...

  7. IOS开发基础知识--碎片9

    1:两种方法删除NSUserDefaults所有记录 //方法一 NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[N ...

  8. iOS tableViewCell自适应高度 第三发类库

    在github中有许多大牛封装好的第三发类库,其中有个自适应cell高度的类库 下载地址:https://github.com/gsdios/SDAutoLayout model类 commentsM ...

  9. python之很好的网站

    1.python官方开发者文档查询和python下载网站 2.

  10. #研发解决方案介绍#基于ES的搜索+筛选+排序解决方案

    郑昀 基于胡耀华和王超的设计文档 最后更新于2014/12/3 关键词:ElasticSearch.Lucene.solr.搜索.facet.高可用.可伸缩.mongodb.SearchHub.商品中 ...