[Leetcode] 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.
题意:计算字符串对应数字的乘积。参考JustDoIT的博客。
思路:先用一维向量存放计算的中间值,中间值存放对应位置乘积的和(暂不考虑进位的情况);然后针对进位的情况,进行计算;注意要跳过开始为0的情况(因为向量开始全部初始为0);然后将数字转化为字符串。以234*156为例:

1 class Solution {
public:
string multiply(string num1, string num2)
{
int len1=num1.size();
int len2=num2.size();
vector<int> midVal(len1+len2,);
int k=len1+len2-;
for(int i=;i<len1;++i)
for(int j=;j<len2;++j)
{
midVal[k-i-j]+=(num1[i]-'')*(num2[j]-''); //从右向左
}
int carry=; //进位
for(int i=;i<len1+len2;++i)
{
midVal[i]+=carry;
carry=midVal[i]/;
midVal[i]%=;
}
int i=len1+len2-;
while(midVal[i]==) //去除向量中最开始的0
i--;
if(i<) return ""; //全部为0时
//将值变成字符串
string res;
for(;i>=;--i)
res.push_back(midVal[i]+'');
return res;
}
};
另外更高效的计算大整数乘法一般有:(1)karatsuba算法,复杂度为3nlog3≈3n1.585,可以参考百度百科、乘法算法-Karatsuba算法。(2)基于FFT(快速傅里叶变换)的算法,复杂度为o(nlogn), 可以参考FFT, 卷积, 多项式乘法, 大整数乘法
[Leetcode] Multiply strings 字符串对应数字相乘的更多相关文章
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- 43. Multiply Strings 字符串相乘
1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode: Multiply Strings 解题报告
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...
- LeetCode OJ:Multiply Strings (字符串乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 043 Multiply Strings 字符串相乘
给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意: num1 和 num2 的长度均小于110. num1 和 num2 均只包含数字 0 ...
随机推荐
- 一道SQL面试题——表行列数据转换(表转置)
SQL语句如下: select country, sum(case when type='A' then money end) as A, sum(case when type='B' then mo ...
- 分分钟教你学习GIt
Git配置: $ git config --global user.name "awen" $ git config --global user.email "awen@ ...
- go 操作数据库
假设有了数据库,创建表 CREATE TABLE `userinfo` ( `uid` INT(10) NOT NULL AUTO_INCREMENT, //自增字段 `username` VARCH ...
- mysql 5.8 查询最新一条数据
SELECT * FROM ( ,) FROM (SELECT * FROM or_task_node ORDER BY created_date DESC) temp ) AS vars ) t g ...
- Python3爬虫(十一) 爬虫与反爬虫
Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.重要概念 二.爬虫反爬虫进化论
- Python3爬虫(六) 解析库的使用之Beautiful Soup
Infi-chu: http://www.cnblogs.com/Infi-chu/ Beautiful Soup 借助网页的结构和属性等特性来解析网页,这样就可以省去复杂的正则表达式的编写. Bea ...
- Java基础——内部类
一.什么是内部类 将一个类定义在另一个类里面或者一个方法里面,这样的类称为内部类 内部类所在的类在编译成功后,会出现这样两个class文件:OuterClass.class和OuterClass$In ...
- 1511: [POI2006]OKR-Periods of Words
1511: [POI2006]OKR-Periods of Words https://www.lydsy.com/JudgeOnline/problem.php?id=1511 题意: 对于一个串的 ...
- linux的常用易忘命令
1.查看软件安装路径 [root@localhost ~]# which gcc /usr/bin/gcc 查询进程 ps -ef |grep redis 查看端口 netstat -lntp |g ...
- spring boot 连接mysql 错误The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one
1.spring boot 整合mybatis 连接mysql时错误 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or repr ...