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 ...
随机推荐
- C# DateTime.Now 详解
//2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...
- 【原创】《从0开始学RocketMQ》—集群搭建
用两台服务器,搭建出一个双master双slave.无单点故障的高可用 RocketMQ 集群.此处假设两台服务器的物理 IP 分别为:192.168.50.1.192.168.50.2. 内容目录 ...
- 解决 iphone5 4 inch 屏 app黑边问题
你需要一张640*1138的预加载图(launch image).在工程>TARGETS 中添加,系统将自动将其重命名为Default-568h@2x.png.
- iOS NSDictionary <--> NSString(JSON) in Objc
NSDictionary --> NSString + (NSString*)stringINJSONFormatForObject:(id)obj { NSData *jsonData = [ ...
- python代码覆盖率coverage简介与用法
如果衡量单元测试对相应代码的测试重量,覆盖率是一个必要非充分条件,因此统计代码的覆盖率,检视单测是否充分,就尤为的重要.这里针对python-unittest的单测的覆盖率coverage进行使用说明 ...
- 转-MAC 下安装PHONEGAP开发环境
来自:http://jinzhe.net/post/8.html 什么是Phonegap呢?Phonegap是一个利用HTML5去开发App的框架.可以为安卓.iOS.WP.黑莓.火狐等移动操作系统. ...
- selenium 延迟等待的三种方式
1.最直接普通的方式:这个是设置固定的等待时间 Thread.sleep(1000); 2.显示等待方式(Explicit Wait):就是明确的要等待的元素在规定的时间之内都没找到,那么就 ...
- Matrix Transformation codechef 数学题
https://www.codechef.com/problems/MTRNSFRM 我只能说codechef的题好劲爆,这题居然是easy的题,太可怕了.而且还有一点就是codechef的题解很难看 ...
- cloudera-scm-server启动出现Error creating bean with name 'entityManagerFactoryBean'与HHH010003: JDBC Driver class not found: com.mysql.jdbc.Driver错误解决办法(图文详解)
不多说,直接上干货! 问题详情 -- ::, INFO main:com.cloudera.server.cmf.Main: Starting SCM Server. JVM Args: [-Dlog ...
- 微信里去掉下拉select的边框
<select name="gender" id="" class=" " style=" -webkit-appeara ...