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的更多相关文章

  1. leetcode面试准备:Multiply Strings

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

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

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

  3. 【LeetCode练习题】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  4. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

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

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

  6. 【一天一道LeetCode】#43. Multiply Strings

    一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...

  7. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  9. LeetCode OJ:Multiply Strings (字符串乘法)

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

随机推荐

  1. 再学Java 之 private、protected、public和default的作用域

    前言:如果提到protected的访问控制级别,您的第一反应是”只能是子类才可以访问“,那么您很可能需要往下看. 首先,大致介绍一下各个访问控制符的访问控制级别(具体的介绍很多大牛的博文都有介绍,我就 ...

  2. 【Java并发编程】:多线程环境中安全使用集合API

    在集合API中,最初设计的Vector和Hashtable是多线程安全的.例如:对于Vector来说,用来添加和删除元素的方法是同步的.如果只有一个线程与Vector的实例交互,那么,要求获取和释放对 ...

  3. fastText 安装

    Windows: https://www.lfd.uci.edu/~gohlke/pythonlibs/#fasttext 下载需要的版本 然后:pip install 文件名

  4. 词性标注 HMM

    1.给定语料,统计语料中的词性为N,词组为M. 2.HMM关注3个参数 a.初始化概率 b.状态转移矩阵 N*N c.观测状态概率矩阵 N*M 3.状态转移矩阵:词A的词性为词性a,词B的词性为词性b ...

  5. springboot-4-整合fastjson

    使用fastjson作为springboot的默认json解析, 原来使用的是jackson 1, 引入依赖 <dependencies> <dependency> <g ...

  6. mysql RR下不存在则插入

    主要看并发事务中不存在则插入(只有key索引)的阻塞情况. 表定义: mysql> desc user; +-------------+------------------+------+--- ...

  7. JAVA-5NIO之Selector

    转载:并发编程网:ifeve.com NIO教程 Selector(选择器)是Java NIO中能够检测一到多个NIO通道,并能够知晓通道是否为诸如读写事件做好准备的组件.这样,一个单独的线程可以管理 ...

  8. awk去重以某列重复的行

    [root@localhost cc]# cat 2.txt adc 3 5 a d aa 3 adfa d ba 3 adf 去重第一列重复的行: [root@localhost cc]# cat ...

  9. oracle 报错归纳总结

    1.ORA-00904 正常情况下是找不到字段(大概率字段名字错了) 还有一种比较特别的情况实在设计DB的时候,字段小写,在sql查询工具自动提示的时候都是大写,造成字段找不到的情况.

  10. mybatis-plus之Mapper CRUD接口和 Service CRUD 接口

    中文官网链接: https://mp.baomidou.com/guide/crud-interface.html