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 ...
随机推荐
- Luogu P3916 图的遍历 【优雅的dfs】【内有待填坑】By cellur925
说明 • 对于60% 的数据, n,m在1e3内 • 对于100% 的数据, n,m在1e5内. 本弱弱上来就是一顿暴搜打,dfs n次,每次更新答案,复杂度为O(n*n),果然TLE,60分抱回家. ...
- 状压入门--bzoj1087: [SCOI2005]互不侵犯King【状压dp】
Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行, ...
- cxf CXF搭建webService服务器
http://observer.blog.51cto.com/4267416/1231205 手动发布: public class ServerMain { public static void ma ...
- 模拟+贪心 SCU 4445 Right turn
题目传送门 /* 题意:从原点出发,四个方向,碰到一个点向右转,问多少次才能走出,若不能输出-1 模拟:碰到的点横坐标相等或纵坐标相等,然而要先满足碰到点最近, 当没有转向或走到之前走过的点结束循环. ...
- 题解报告:hdu 2058 The sum problem
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2058 问题描述 给定一个序列1,2,3,...... N,你的工作是计算所有可能的子序列,其子序列的总 ...
- Service官方教程(9)绑定服务时的注意事项
Binding to a Service Application components (clients) can bind to a service by calling bindService() ...
- Python快速教程(转载)
Python快速教程 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 怎么能快速地掌握Python?这是和朋友闲聊时谈起的问题 ...
- thinkphp3.2.3连接sqlserver 2008 R2 数据库
环境: 操作系统——win7 64位旗舰版 PHP——thinkphp 3.23 数据库——Microsoft SQL Server 2008 R2 需要用到的软件: 步骤: 1.搜索SQLSRV30 ...
- Java开发笔记(九十三)深入理解字节缓存
前面介绍了文件通道的读写操作,其中用到字节缓存ByteBuffer,它是位于通道内部的存储空间,也是通道唯一可用的存储形式.ByteBuffer有两种构建方式,一种是调用静态方法wrap,根据输入的字 ...
- YOLOv3模型识别车位图片的测试报告(节选)
1,YOLOv3模型简介 YOLO能实现图像或视频中物体的快速识别.在相同的识别类别范围和识别准确率条件下,YOLO识别速度最快. 官网:https://pjreddie.com/darknet/yo ...