LeetCode题解之Multiply Strings
1、题目描述

2、问题分析
按照手算乘法的过程进行计算,首先计算乘法,然后计算加法。
3、代码
string multiply(string num1, string num2) {
string small ;
string big;
if( num1.size() < num2.size() ){
small = num1;
big = num2;
}else if( num2.size() < num1.size() ){
small = num2;
big = num1;
}else{
small = num1;
big = num2;
}
int n = small.size() ;
vector<string> re;
string::reverse_iterator it_s = small.rbegin() ;
while( it_s != small.rend() ){
string s;
int up = ;
string::reverse_iterator it_b = big.rbegin() ;
while( it_b != big.rend() ){
int m = (*it_s - '') * (*it_b - '') + up;
if( m < ){
s = std::to_string(m) + s;
up = ;
}else{
s = std::to_string( m% ) + s;
up = m/;
}
++it_b;
}
if( up != ){
s = std::to_string( up ) + s;
}
re.push_back( s );
++it_s;
}
string zero;
for( vector<string>::iterator it = re.begin() ; it != re.end() ; it++ ){
*it += zero;
zero += "";
}
string result = "" ;
for( int i = ; i < re.size() ; i++ ){
result = binaryAdd( result , re[i] );
}
int q = ;
for( string::iterator it = result.begin() ; it != result.end() ; ++it ){
q += (*it - '');
}
if( q == )
result = "";
return result;
}
string binaryAdd( string s1 , string s2 ){
string s;
string::reverse_iterator it1 = s1.rbegin();
string::reverse_iterator it2 = s2.rbegin();
int up = ;
while( it1 != s1.rend() && it2 != s2.rend() ){
int a = (*it1 - '') + (*it2 - '') + up;
if( a < ){
s = std::to_string(a) + s;
up = ;
}else{
s = std::to_string( a - ) + s;
up = ;
}
++it1 ;
++it2 ;
}
while( it1 != s1.rend() ){
if( *it1 - '' + up < ){
s = std::to_string( *it1 - '' + up ) + s;
up = ;
}else{
s = std::to_string( *it1 - '' + up - ) + s;
up = ;
}
++it1;
}
while( it2 != s2.rend() ){
if( *it2 - '' + up < ){
s = std::to_string( *it2 - '' + up ) + s;
up = ;
}else{
s = std::to_string( *it2 - '' + up - ) + s;
up = ;
}
++it2;
}
if( up == ){
s = std::to_string() + s;
}
return s;
}
LeetCode题解之Multiply Strings的更多相关文章
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- 【LeetCode练习题】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- leetcode个人题解——#43 Multiply Strings
思路:高精度乘法就可以了. 有两个错误以前没在意,1.成员属性定义时候不能进行初始化, vector<); 这样隐性调用了函数进行初始化的形式特别要注意,也是错误的: 2.容器类只有分配了空间时 ...
- 【一天一道LeetCode】#43. Multiply Strings
一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode:43. Multiply Strings (Medium)
1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...
- LeetCode OJ:Multiply Strings (字符串乘法)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
随机推荐
- 一桩由X509Certificate2引发的血案
A process serving application pool '. The data field contains the error number. 在某次网站更新后,发现wcf服务不可用了 ...
- javascript双等号引起的类型转换
隐性类型转换步骤 一.首先看双等号前后有没有NaN,如果存在NaN,一律返回false. 二.再看双等号前后有没有布尔,有布尔就将布尔转换为数字.(false是0,true是1) 三.接着看双等号前后 ...
- Android硬件抽象层(HAL)深入剖析(三)【转】
前面分析了android HAL层是如何搜索硬件模块的动态共享库的,其实就是在"system/lib/hw/"或者"/vendor/lib/hw/"这两个路径下 ...
- 03-python的新式类和经典类区别
新式类就是 class person(object): 这种形式的, 从py2.2 开始出现的 新式类添加了: __name__ is the attribute's name. __doc__ i ...
- elasticsearch 导入基础数据并索引之 geo_shape
我们看到的图形, 实际是由点来完成的, 有2种类型的格子模型可用于地理星座, 默认使用的是geoHash, 还有一种4叉树(quad trees), 也可用于 判断形状与索引的形状关系 1), int ...
- Java 注解实例
package com.annotation; import java.lang.annotation.Retention; import java.lang.annotation.Target; i ...
- Python List 基础学习
list&tuple&dict list list 常见操作 初始化: list1 = [123, 'abc', 4.56, ['inner', 'list'], 7-9j] list ...
- Python引用复制,参数传递,弱引用与垃圾回收
引用 先上个示例: >>> val = [1] >>> val[0] = val >>> val [[...]] 上述代码使val中包含自身,而产 ...
- SQL SERVER 原来还可以这样玩 FOR XML PATH
FOR XML PATH 有的人可能知道有的人可能不知道,其实它就是将查询结果集以XML形式展现,有了它我们可以简化我们的查询语句实现一些以前可能需要借助函数活存储过程来完成的工作.那么以一个实例为主 ...
- Javascript Madness: Mouse Events
http://unixpapa.com/js/mouse.html Javascript Madness: Mouse Events Jan WolterAug 12, 2011 Note: I ha ...