题目: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.

就是实现大数乘法。嘿嘿,我先用long long直接乘试试手感,试过了不行。所以还是要用字符串表示才行。

我是这样做的,先实现两个子函数,一个是实现一个数和一个字符串的数相乘的结果。另一个是实现两个字符串相加的结果。这样,大数乘法就可以由其中一个大数的每一位和另一个大数相乘然后再相加,就是要注意分解一个大数的时候记得要在结果后面加上零。例如12乘以3,我们把12分成1和2分别和3相乘,然后1和3相乘的后面要加一个零,因为1处于12的十位数。

oj上居然用了148ms。不忍直视啊。写了那么长。。。虽然Accept了

class Solution {
public:
string singleMulti(string s, char a) // 给定一个字符串和一个字符,返回乘积(字符串形式)
{
if (s.size() == )
return s;
if (a == '')
return "";
int c = a - '';
int up = ;
for (int i = s.size() - ; i > -; --i)
{
int tmp = s[i] - '';
int sum_now = up + c*tmp;
s[i] = sum_now% + '';
up = sum_now/;
}
if (up != )
{char tmp = up + '';
s = tmp + s;}
return s;
}
string sum42(string s1, string s2) // sum of two string
{
if (s1.size() == )
return s2;
if (s2.size() == )
return s1;
string s = s1;
int len1 = s1.size() - , len2 = s2.size() - , up = ;
while(len1 > - && len2 > -)
{
int n1 = s1[len1] - '';
int n2 = s2[len2] - '';
int n3 = n1 + n2 + up;
s[len1] = n3% + '';
up = n3/;
len1--;len2--;
}
while(len1 > -)
{
int n1 = s1[len1] - '';
int n3 = n1 + up;
s[len1] = n3% + '';
up = n3/;
len1--;
}
while(len2 > -)
{
int n2 = s2[len2] - '';
int n3 = n2 + up;
char tmp = n3% + '';
s = tmp + s;
up = n3/;
len2--;
}
if(up)
{
char tmp = up + '';
s = tmp + s;
}
return s;
}
string multiply(string num1, string num2)
{
if (num1 == "" || num2 == "")
return "";
int len1 = num1.size()-;
string s;
int cnt = ;
while(len1 > -)
{
string tmp = "";
int tn = cnt;
while(tn)
{
tmp = '' + tmp;
tn--;
}
s = sum42(s,singleMulti(num2, num1[len1])+ tmp);
len1--;
cnt++;
}
return s;
}
};

由于自己的代码那么长,所以也学习了下别人的,例如这里利用下面的图,很好理解。将图贴出:

我们以289*758为例

按照这个图我写了下代码(跟原版主略有不同):

class Solution {
public:
string multiply(string num1, string num2)
{
if (num1 == "" || num2 == "")
return "";
string s = "";
int len1 = num1.size(), len2 = num2.size();
vector<int> container(len1+len2, ); // 用来存表中红色部分值 for (int i = ; i < len1; ++i)
for (int j = ; j < len2; ++j)
{
container[i+j] += (num1[len1 - - i]-'')*(num2[len2 - - j]-''); // 注意标号
}
//处理进位
int up = , cnt = ;
while(cnt<len1+len2)
{
container[cnt] += up;
up = container[cnt]/;
container[cnt] %= ;
cnt++;
}
cnt--;
while(cnt > - && !container[cnt])//不应该有的零去掉
{
cnt--;
};
while(cnt > -) // 输出就是结果,注意方向被搞反了
{
char ch = container[cnt] + '';
s += ch;
cnt--;
}
return s;
}
};

这个主要是注意存的方向,不要把小标弄混淆了。输出的时候也要注意,别搞反了。低于60ms过。

leetcode 第42题 Multiply Strings的更多相关文章

  1. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

  2. [LeetCode] 大数问题,相加和相乘,题 Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  3. LeetCode(43)Multiply Strings

    题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...

  4. leetcode个人题解——#43 Multiply Strings

    思路:高精度乘法就可以了. 有两个错误以前没在意,1.成员属性定义时候不能进行初始化, vector<); 这样隐性调用了函数进行初始化的形式特别要注意,也是错误的: 2.容器类只有分配了空间时 ...

  5. LeetCode第[42]题(Java):Trapping Rain Water (数组方块盛水)——HARD

    题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings

    1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...

  8. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

  9. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

随机推荐

  1. yii组态 redis主从配置(随着代码)

    最近的欲望redis 主从,但yii内建的redis 它不支持主从.不仅写了一个好办法 结构例,以下: 1.main.php通过添加下面的句子: //redis缓存配置 'cache_redis' = ...

  2. SQL Server 2008 R2 性能计数器详细列表(二)

    原文:SQL Server 2008 R2 性能计数器详细列表(二) SQL Server Buffer Partition 对象: 提供计数器来监视 SQL Server 如何使用可用页 SQL S ...

  3. [python]自问自答:python -m参数? (转)

    python -m xxx.py 作用是:把xxx.py文件当做模块启动但是我一直不明白当做模块启动到底有什么用.python xxx.py和python -m xxx.py有什么区别! 自问自答: ...

  4. height/innerHeight/outerHeight

    <script> $(document).ready(function(){ alert("height:"+$("#div").height()) ...

  5. (大数据工程师学习路径)第一步 Linux 基础入门----简单的文本处理

    介绍 这一节我们将介绍这几个命令tr(注意不是tar),col,join,paste.实际这一节是上一节关于能实现管道操作的命令的延续,所以我们依然将结合管道来熟悉这些命令的使用. 一.常用的文本处理 ...

  6. MyEclipse中将项目导出jar包,以及转化成EXE文件

    1.对于项目如何导出jar文件,和生成exe,解答目录如下: 首先生成jar文件,单击项目名称CF-users(这是我的协同过滤项目文件名称)右击--->Export如下图: 单击下一步 Sel ...

  7. jQuery简要dom操作

    文本 dom 获取标签 $(选择). 创建一个标签对象 $("标签"): 由于所有的返回jQuery对象,能够调用链(无论jQuery API 回报jQuery对象) 插入标签 内 ...

  8. Codeforces Round #277.5 (Div. 2)A——SwapSort

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  9. oracle HA 高可用性具体解释(之中的一个)

    oracle HA 高可用性具体解释(之二,深入解析TAF,以及HA框架) :http://blog.csdn.net/panfelix/article/details/38436197 一.HA F ...

  10. 【剑指offer】最大和连续子阵列

    个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...