LeetCode(43)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.
分析
计算两个字符串表示的非负大整数的乘积,结果仍然用字符串表示。
我们都熟悉笔算的整数乘积,按照被乘数逐位与乘数求积,保存进位;当被乘数换位时,结果递增一个数量级,与先前结果求和。
123
*
456
————————————
738
615
492
————————————
56088
如上例所示,当我们计算字符串“123”*“456”时,采取与笔算相同的思想,按照从个位—>高位的计算思想,现将字符串翻转,计算完成后,翻转结果,返回。
AC代码
class Solution {
public:
string multiply(string num1, string num2) {
//如果有其中一个乘数的字符串表示为空,则返回空字符串
if (num1.empty() || num2.empty())
return string();
if (num1 == "0" || num2 == "0")
return "0";
//按照整数从低位到高位计算,翻转两个乘数字符串
reverse(num1.begin(), num1.end());
reverse(num2.begin(), num2.end());
//求两个乘数字符串的长度
int len1 = strlen(num1.c_str()), len2 = strlen(num2.c_str());
//定义乘法结果字符串
string ret = "";
//保存进位
int carry = 0;
for (int i = 0; i < len1; i++)
{
//乘数的处理起始位
size_t pos = i;
for (int j = 0; j < len2; j++)
{
int temp = (num1[i] - '0') * (num2[j] - '0') + carry;
//向当前位加入结果
if (pos < ret.length())
{
temp = temp + (ret[pos] - '0');
ret[pos] = temp % 10 + '0';
}//if
else{
ret.append(1, temp % 10 + '0');
}//else
//计算下一个进位
carry = temp / 10;
//处理乘数的下一位
pos++;
}//for
if (carry > 0)
ret.append(1, carry + '0');
carry = 0;
}//for
//翻转整个结果字符串
reverse(ret.begin(), ret.end());
return ret;
}
};
LeetCode(43)Multiply Strings的更多相关文章
- LeetCode(43):字符串相乘
Medium! 题目描述: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = &quo ...
- LeetCode(205)Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...
- Windows Phone开发(43):推送通知第一集——Toast推送
原文:Windows Phone开发(43):推送通知第一集--Toast推送 好像有好几天没更新了,抱歉抱歉,最近"光荣"地失业,先是忙于寻找新去处,唉,暂时没有下文.而后又有一 ...
- SQL Server高可用——日志传送(4-3)——使用
原文:SQL Server高可用--日志传送(4-3)--使用 顺接上一篇:SQL Server高可用--日志传送(4-2)--部署 本文为本系列最重要的一篇,讲述如何使用日志传送及一些注意事项.从上 ...
- Qt 学习之路 2(43):QStringListModel
Qt 学习之路 2(43):QStringListModel 豆子 2013年2月13日 Qt 学习之路 2 38条评论 上一章我们已经了解到有关 list.table 和 tree 三个最常用的视图 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
随机推荐
- 设置mysql 定时备份任务
1 修改配置文件 /etc/my.conf (为了命令 mysqldump能省略输入密码执行,mysql5.5 之后已经不建议控制台直接输入密码的方式) 增加如下配置 [client]host=lo ...
- 解决Robot Framework运行时没有Log的方案
Robot Framework自动化测试过程中,运行多次后会出现RIDE没有log的情况. 造成这种现象的原因是: 执行失败的测试用例,chrome.exe和chromedriver.exe进程没有关 ...
- Vasiliy's Multiset CodeForces -706D || 01字典树模板
就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 #include<cstdio> #include<algorithm> using namespace st ...
- Linux tcpdump命令英文文档
https://www.computerhope.com/unix/tcpdump.htm
- 升序 Collections.sort(list) 降序 Collections.reserve(list) 随机 Collections.shuffle(list)
package Day28ketangzuoye; import java.util.ArrayList; import java.util.Collections; import java.util ...
- MVP架构模式
概念解释 MVP是Model(数据) View(界面) Presenter(表现层)的缩写,它是MVC架构的变种,强调Model和View的最大化解耦和单一职责原则 Model:负责数据的来源和封装, ...
- 树莓派 VNC 远程桌面 同一个桌面
如何在ssh登录的情况下配置好vino 1.传输文件 2.ssh sudo dpkg -i ~/swap/deb/tight* sudo cp ~/swap/vino.desktop /etc/xdg ...
- [转]Resolve Team Foundation Version Control conflicts
本文转自:http://msdn.microsoft.com/en-us/library/ms181432.aspx An advantage of using Team Foundation ver ...
- android开发学习——facebook第三方登录,看了你不会后悔
给APP用原生android进行facebook第三方登录. 我们做一件事情,首先得了解其原理,这样才不会迷茫,才知道自己做到什么程度了,心里才会有底. 所以,第一步,了解第三方登录的原理:下面贴一些 ...
- mysql之通过cmd连接远程数据库
---恢复内容开始--- 目录 前提 连接远程数据库 前提: 本地安装了mysql数据库 本地和远程网络是连通的,通过命令ping ip (即ping 192.168.0.333),可以ping通 连 ...