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.

将乘积逐位逆序存放在int数组result中。

记num1当前为第i位,num2当前为第j位,则乘积存放在result[(n1-1-i)+(n2-1-j)]中。

注意进位,当num1[i]与num2的每一位都相乘之后,如果仍有进位,需要存放在result[(n1-1-i)+n2]中。

最后将result逆序,去掉前面的0.但是如果全为0就返回"0"。

class Solution {
public:
string multiply(string num1, string num2) {
int n1 = num1.size();
int n2 = num2.size();
vector<int> result(n1+n2);
string resultStr; for(int i = n1-; i >= ; i --)
{// for num1[i]
int carry = ;
int val1 = num1[i] - '';
for(int j = n2-; j >= ; j --)
{// for num2[j]
int val2 = num2[j] - '';
int res = val1 * val2;
int ind = (n1--i)+(n2--j);
result[ind] += carry;
result[ind] += res; carry = result[ind] / ;
result[ind] %= ;
}
if(carry != )
{
int ind = (n1--i)+n2;
result[ind] += carry;
}
}
//reverse result
reverse(result.begin(), result.end());
int i;
for(i = ; i < n1+n2; i ++)
{
if(result[i] != )
break;
}
if(i == n1+n2)
//all 0
return "";
else
{
for(; i < n1+n2; i ++)
{
resultStr += (result[i] + '');
}
return resultStr;
}
}
};

【LeetCode】43. Multiply Strings的更多相关文章

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

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

  2. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

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

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

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

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

  5. 【LeetCode题意分析&解答】43. Multiply Strings

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

  6. LeetCode:43. Multiply Strings (Medium)

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

  7. 【LeetCode】205. Isomorphic Strings 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典保存位置 字典保存映射 日期 题目地址:http ...

  8. 【LeetCode】859. Buddy Strings 解题报告(Python & C++)

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

  9. 【LeetCode】205. Isomorphic Strings

    题目: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c ...

随机推荐

  1. Sequential projection learning for hashing阅读笔记

    真不能再挖坑了,前面挖聊很多坑都没来得及填,从今往后,能写多少就是多少.Sequential projection learning for hashing这篇文章去年就阅读了,当时阅读完没来得及做笔 ...

  2. CoreAudio实现录音播音和扬声器听筒模式的切换

    本例子使用Core Audio实现类似于微信的音频对讲功能,可以录音和播放并且实现了听筒模式和扬声器模式的切换.录音主要使用AVAudioRecorder类来实现录音功能,播放则使用AVAudioPl ...

  3. 混沌数学之Kent模型

    相关软件:混沌数学之离散点集图形DEMO 相关代码: // http://wenku.baidu.com/view/7c6f4a000740be1e650e9a75.html // 肯特映射 clas ...

  4. Ubuntu Linux自动发邮件配置及邮件发送脚本

    测试环境:Ubuntu 11.10 1. 安装mutt及msmtp软件 sudo apt-get install mutt sudo apt-get install msmtp 2. 编辑配置文件vi ...

  5. Android ToggleButton Example--开关按钮

    Android ToggleButton Example 在 Android 中,  “android.widget.ToggleButton” 是个特殊的类,可以渲染出一个“开关按钮” ,顾名思义, ...

  6. SQL基础(四):SQL命令

    1.CREATE INDEX 语句 CREATE INDEX 语句用于在表中创建索引.在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据. 索引:在表中创建索引,以便更加快速高效地查询数据 ...

  7. HDU1161 Eddy&#39;s mistakes

    Eddy's mistakes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. matlab中help所有函数功能的英文翻译

    doc funname 在帮助浏览器中打开帮助文档 help funname 在命令窗口打开帮助文档 helpbrowser 直接打开帮助浏览器 lookfor funname 搜索某个关键字相关函数 ...

  9. UILabel字体间距调整

    思路: 写一个 UILbel的子类:在子类里面重新布置UILbel的字体间距: 如代码 .h #import <UIKit/UIKit.h> @interface AdjustableUI ...

  10. BeautifulSoup的成员结构

    >>> dir(soup)['ASCII_SPACES', 'DEFAULT_BUILDER_FEATURES', 'HTML_FORMATTERS', 'ROOT_TAG_NAME ...