【LeetCode练习题】Multiply Strings
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.
题目意思:
给两个string,计算string的乘积。
string中的数可以非常大并且是非负数。
(就是大数的乘法运算嘛。。。)
解题思路:
由于之前已经练习过了大数的加减乘除四则运算了,所以这道题跟那个是一样的原理。
要想实现乘法,首先得实现大数的加法,因为按照小学摆竖式计算乘法的时候需要在会加法的基础上才行。
于是我们首先实现了大数的加法,基本上就是模拟了用竖式每一位相加的过程,其中注意到产生的进制要参与高位的运算。
实现了加法后,乘法也就出来了。
代码如下:
string operator+(const string &num1,const string &num2){
int nCarry = ;
string numTemp; int i = num2.size() - ;
int j = num1.size() - ;
for(; i >= || j >= ; i--,j--){
char a,b;
if(i>=)
b = num2[i] - '';
else
b = ;
if(j>=)
a = num1[j] - '';
else
a = ;
char c = a + b + nCarry;
nCarry = c / ;
numTemp.push_back(c% + '');
} if(nCarry != ){
numTemp.push_back(nCarry + '');
} for(i = , j = numTemp.size() - ; i < j; i++,j--){
char cTemp = numTemp[i];
numTemp[i] = numTemp[j];
numTemp[j] = cTemp;
}
return numTemp;
}
string operator*(const string &num1,const string &num2){
int nCarry = ;
string numTemp;
string result; int i = num2.size()-;
for(; i >= ; i--){
char a = num2[i] - '';
int j = num1.size() - ;
for(; j >= ; j--){
char b = num1[j] - '';
char c = b * a + nCarry;
nCarry = c/;
numTemp.push_back(c % + '');
}
if(nCarry != ){
numTemp.push_back(nCarry + '');
nCarry = ;
} //reverse
int n = ;
int m = numTemp.size() - ;
for(; n < m; n++,m--){
char cTemp = numTemp[n];
numTemp[n] = numTemp[m];
numTemp[m] = cTemp;
} for(int t = num2.size() - ; t > i; t--)
{
numTemp.push_back('');
} result = result + numTemp;
numTemp.clear();
}
return result;
} class Solution {
public:
string multiply(string num1, string num2) {
string ret;
string zero = "";
if(!num1.compare(zero) || !num2.compare(zero)){
ret = "";
return ret;
}
ret = num1 * num2;
return ret;
}
};
【LeetCode练习题】Multiply Strings的更多相关文章
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode:Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
随机推荐
- ArcGIS for WPF 访问外部资源
原文 http://www.cnblogs.com/wdysunflower/archive/2011/07/14/2105584.html ArcGIS for WPF 访问外部资源 应用背景: 因 ...
- Bash Shell 快捷键的学习使用
原文地址: http://dbanotes.net/tech-memo/shell_shortcut.html 这篇 Bash Shell Shortcuts 的快捷键总结的非常好.值得学习.下面内容 ...
- N-Queens 解答
Question The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no tw ...
- IOS 网络判断
Reachability *connectionNetWork= [Reachability reachabilityForInternetConnection] ; int status = [co ...
- 所闻所获5:关于iOS的证书
去年做ondine时,被iOS的证书搞得很是头大,做完了之后感觉一片混乱,印象也不是很深.最近又发布了meditashayne,个人的第二个App,也就重温了一下证书的一些相关操作.这一次的理解比较深 ...
- Java正則表達式
近期工作中常常要用到正則表達式,不得不花点时间对其进行一定的学习. JDK中提供了2个类来支持正則表達式,各自是java.util.regex.Pattern和java.util.regex.Ma ...
- PCL库配置出现的问题(WIN10+VS2013)
边看电影边配终于配好了,中间出现了一些问题,在网上很难搜到,可能每个人都碰到的不同.摸索了一会终于都解决了,记录在这里,免得又碰到. PCL是什么东西就不在此介绍了. 主要是参考这篇博客做得,不过我后 ...
- [Python笔记][第三章Python选择与循环]
1月28日学习 Python选择与循环 选择结构 多分枝选择结构 if 表达式 1: 语句块 1 elif 表达式 2: 语句块 2 elif 表达式 3: 语句块 3 ... else : 语句块 ...
- oc特有语法
分类 问题 1.什么是分类? 就是把一个类的功能,分出一部分来放在一个独立的文件中 2.分类的语法是什么样的? @interface Person(SuperMan) 3.分类与类是什么关系? 分类依 ...
- php.ini配置
PHP作为一门强大的脚本语言被越来越多的web应用程序采用,不规范的php安全配置可能会带来敏感信息泄漏.SQL注射.远程包含等问题,规范的安全配置可保障最基本的安全环境.下面我们分析几个会引发安全问 ...