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 ...
随机推荐
- margin折叠及hasLayout && Block Formatting Contexts
margin折叠的产生有几个条件: 这些margin都处于普通流中,并在同一个BFC中: 这些margin没有被非空内容.padding.border 或 clear 分隔开: 这些margin在垂直 ...
- C/C++ -- Gui编程 -- Qt库的使用 -- Qt编码问题
1.直接使用QObject::trUtf8("中文字符串") 2.头文件<QTextCodec>QTextCodec::setCodecForTr(QTextCodec ...
- elasticsearch(一):JAVA api操作
1.创建一个mavan项目,项目的以来配置如下. <?xml version="1.0" encoding="UTF-8"?> <projec ...
- 小程序之底部tabBar
用法简介: 1.app.json中配置下tabBar即可,注意tabBar至少需要两个最多五个Item选项 这里简单列举一些属性值:对于tabBar整体属性设置: 对于tabBar中每个Item属性设 ...
- css3的overflow-anchor
overflow-anchor属性使我们能够选择退出滚动锚定,这是一个浏览器特性,旨在允许内容在用户当前的DOM位置上加载,而不需要在内容完全加载后更改用户的位置. 为何要有这个属性? 滚动锚定是一种 ...
- VB如何连接访问数据库Access
VB如何连接访问数据库Access 听语音 | 浏览:10675 | 更新:2015-05-05 11:26 | 标签:连接 access 1 2 3 4 5 6 7 分步阅读 VB即Visual B ...
- 新手之首次部署阿里云centos7+mysql+asp.net mvc core应用之需要注意的地方
先来几个字,坑坑坑. 自己业余爱好者,签名一直捣鼓net+mssql,前阵买了阿里云esc,自己尝试做个博客,大体架子都打好了,本地安装了mysql,测试了也没问题. 部署到阿里云centos7,结果 ...
- MVC初级知识之——Routing路由
实例产品基于asp.net mvc 5.0框架,源码下载地址:http://www.jinhusns.com/Products/Download 我们注意到地址栏的URL是Home/Index 路由可 ...
- 008.在C#中,显式接口VS隐式接口
原文http://www.codeproject.com/Articles/1000374/Explicit-Interface-VS-Implicit-Interface-in-Csharp (At ...
- 我在项目中运用 IOC(依赖注入)--实战篇
上一篇<我在项目中运用 IOC(依赖注入)--入门篇>只是简单的使用 IOC.实际项目使用 IOC 的情景复杂多了,比如说,构造函数有多个参数,有多个类继承同一个接口... Unity都有 ...