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 ...
随机推荐
- 【Java并发编程】:多线程环境中安全使用集合API
在集合API中,最初设计的Vector和Hashtable是多线程安全的.例如:对于Vector来说,用来添加和删除元素的方法是同步的.如果只有一个线程与Vector的实例交互,那么,要求获取和释放对 ...
- .gitignore释疑
自己总结: *.a--匹配任意路径下的.a文件 a/--匹配任意路径下的a文件夹 a/*--匹配根目录下的文件夹a下的任意文件(等同于 /a/* 或 /a/) ----一般情况下不会有这种写法 /a ...
- 【数组】Minimum Size Subarray Sum
题目: Given an array of n positive integers and a positive integer s, find the minimal length of a sub ...
- es索引的RestHighLevelClient实现
java代码: import java.io.IOException; import org.apache.http.HttpHost; import org.elasticsearch.action ...
- C语言——<计算>_较大两个数相乘
例题:9876543210*1234567890 的乘积 分析:正常的数据结构已经无法满足这么大的数相乘的结果.只能使用数组来进行操作. 1.两个数都用字符数组来接收. 2.接收后,因为每一位要乘以另 ...
- CocoaPods管理第三方
之前听伟哥说用CocoaPods做第三方库的管理很方便,今天看了下自己做了下感觉确实不错.下面开始,Let's go!! 1.安装CocoaPods之前,先确保本地有Ruby环境,因为CocoaPod ...
- 17.async 函数
async 函数 async 函数 含义 ES2017 标准引入了 async 函数,使得异步操作变得更加方便. async 函数是什么?一句话,它就是 Generator 函数的语法糖. 前文有一个 ...
- Ionic3 UI组件之 Gallery Modal
Gallery Modal可以理解为相册的预览界面.可以显示网络图片,也可以显示base64Image. 在这个例子中,我用来实现图片的预览功能. 相机拍照,或者相册选择图片后,用缩略图组件显示缩略图 ...
- Spring Aop之@Before、@After、@Around、@AfterReturning
在项目中使用到了@Aspect注解,故研究了下与其配套的几个注解,将测试结果记录下来 import org.aspectj.lang.JoinPoint; import org.aspectj.lan ...
- 项目托管到Github上
一.注册github账号 首先需要注册一个github账号,注册地址:https://github.com 接着会来到这 然后会收到一封github发的邮件,进入邮箱验证 二.创建个人的githu ...