题目

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; }
};

GitHub测试程序源码

LeetCode(43)Multiply Strings的更多相关文章

  1. LeetCode(43):字符串相乘

    Medium! 题目描述: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = &quo ...

  2. LeetCode(205)Isomorphic Strings

    题目 Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the ch ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...

  4. Windows Phone开发(43):推送通知第一集——Toast推送

    原文:Windows Phone开发(43):推送通知第一集--Toast推送 好像有好几天没更新了,抱歉抱歉,最近"光荣"地失业,先是忙于寻找新去处,唉,暂时没有下文.而后又有一 ...

  5. SQL Server高可用——日志传送(4-3)——使用

    原文:SQL Server高可用--日志传送(4-3)--使用 顺接上一篇:SQL Server高可用--日志传送(4-2)--部署 本文为本系列最重要的一篇,讲述如何使用日志传送及一些注意事项.从上 ...

  6. Qt 学习之路 2(43):QStringListModel

    Qt 学习之路 2(43):QStringListModel 豆子 2013年2月13日 Qt 学习之路 2 38条评论 上一章我们已经了解到有关 list.table 和 tree 三个最常用的视图 ...

  7. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  8. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. JavaScript 数组相关基础方法

    文章来源于:https://www.cnblogs.com/dolphinX/p/3353590.html 创建数组 构造函数 1.无参构造函数,创建一空数组 var a1=new Array(); ...

  2. VS2019 字符串对指针char*赋值编译器报错原因及解决方法

    2019-05-26   21:55:08 前几天在敲代码时,将字符串“Hellow world!”赋值给指针char*类型指针时编译器报错的问题 网上搜索后发现 char*是历史遗留问题,如果程序修 ...

  3. 图论算法->最短路

    求最短路算法,有Floyd,dijkstra,Bellmanford,spfa等诸多高级算法.优化方法也是层出不穷. 我们不妨分析一下各算法的使用特点(可能不准确 1.Floyd算法 复杂度O(n³) ...

  4. 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)

    Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...

  5. Create the first sql server 2016 mobile report;创建 第一个 sqlserver 2016 Mobile report

    在微软收购了datazen之后,sqlserver2016 集成了mobilereport,mobile report 基于html5,兼容各类主流浏览器,之前ssrs2008 R2中很多chart类 ...

  6. android将对象序列化到文件:直接写文件与用Serializable接口的对比

    1.用文件读写1024个对象的日志 10-09 16:12:44.493 6385-6385/com.example.tt.downtest D/Serializable_TAG: write 102 ...

  7. Snort里的规则目录文件解读(图文详解)

    不多说,直接上干货! snort的规则啊,是基于文本的,它通常存在于snort程序目录中或者子目录中,规则文件按照不同的组,进行分类存放的. snort的安装目录 [root@datatest sno ...

  8. [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)

    本文转自:http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/sorting-fi ...

  9. 【Hibernate】Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

    今天用hibernate框架写crm项目时遇到报错: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' n ...

  10. css3中的变换、动画和过渡

    变换:分为2d变换和3d变换,但一次只能用一个变换属性,多个的话最后一个会覆盖前面所有最终被浏览器实现,变换可以成为过渡和动画的一个待变参数(类似width,opacity等). 过渡:是动画的初始模 ...