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.

string multiply(string& num, char ch){
int n = ch - '';
string s;
int carry = ;
int x;
for(int i=num.size()-; i>=; i--){
x = (num[i]-'') * n + carry;
carry = x/;
s.insert(s.begin(), x%+'');
}
if (carry>) {
s.insert(s.begin(), carry+'');
}
return s;
} string strPlus(string& num1, string& num2) {
string s;
int carry=;
int x;
int n1 = num1.size();
int n2 = num2.size(); int i, j;
for(i=n1-, j=n2-; i>= || j>=; i--, j--){
int x1 = i>= ? num1[i]-'' : ;
int x2 = j>= ? num2[j]-'' : ;
x = x1 + x2 + carry;
carry = x/;
s.insert(s.begin(), x%+'');
}
if (carry>) {
s.insert(s.begin(), carry+'');
}
return s;
} string multiply(string num1, string num2) { if (num1.size()<= || num2.size()<=) return ""; int shift=;
string result="";
for (int i=num1.size()-; i>=; i--) {
string s = multiply(num2, num1[i]);
for(int j=; j<shift; j++){
s.insert(s.end(), '');
}
result = strPlus(result, s);
shift++;
}
//check if it is zero
if (result[]=='') return "";
return result;
}

43. Multiply Strings 字符串表示的大数乘法的更多相关文章

  1. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  2. [LeetCode] 43. Multiply Strings 字符串相乘

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

  3. 43. Multiply Strings 字符串相乘

    1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...

  4. Leetcode43. Multiply Strings字符串相乘(大数相乘)

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...

  5. 43. Multiply Strings字符串相乘

    网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

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

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

  8. Multiply Strings 字符串相乘

    http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...

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

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

随机推荐

  1. P2455 [SDOI2006]线性方程组(real gauss)

    P2455 [SDOI2006]线性方程组 (upd 2018.11.08: 这才是真正的高斯消元模板) 找到所消未知数(设为x)系数最大的式子,把它提上来 把这个式子的 x 系数约成1 把这个式子用 ...

  2. Arduino使用HC05蓝牙模块与手机连接

    Arduino使用HC05蓝牙模块与手机连接 一切都是最好的选择 首先是线路连接,一定不要接错了 Arduino 代码 #include <SoftwareSerial.h> // Pin ...

  3. Class<T>

    首先,什么是类类型? 可见: https://www.cnblogs.com/yanze/p/9717658.html Class<T>即T的类类型 如何获取Class<T>? ...

  4. 20145313张雪纯MSF基础应用实验

    实验博客 ms08_067攻击实验 http://www.cnblogs.com/entropy/p/6690301.html ms11_050漏洞攻击 http://www.cnblogs.com/ ...

  5. codevs1001 舒适的路线 - 贪心 - 并查集

    题目描述 Description Z小镇是一个景色宜人的地方,吸引来自各地的观光客来此旅游观光.Z小镇附近共有N(1<N≤500)个景点(编号为1,2,3,…,N),这些景点被M(0<M≤ ...

  6. redis.conf 配置 详解 中文 2.8

        # redis version 2.8.19   # 1k => 1000 bytes# 1kb => 1024 bytes# 1m => 1000000 bytes# 1m ...

  7. dp问题 -挑战例题 2017-7-24

    01 背包 题意: 在N件物品取出若干件放在容量为W的背包里,每件物品的体积为W1,W2……Wn(Wi为整数),与之相对应的价值为P1,P2……Pn(Pi为整数).求背包能够容纳的最大价值. f[i] ...

  8. Linux(CentOS 6.5) 下安装MySql 5.7.18 二进制版本粗浅攻略

    鉴于Linux和mysql因不同版本,安装方式也不同,所以在阅读本攻略前,请确保各位同学的版本和我的Linux.MySql 版本一致. 如果不一致,只能参考. 我的版本: Linux CentOS 6 ...

  9. python 深拷贝

    from copy import deepcopy #create a tuple tuplex = (, [], True) print(tuplex) #make a copy of a tupl ...

  10. c++ 返回指定元素连续相等的位置索引(equal_range)

    #include <iostream> // cout #include <algorithm> // equal_range, sort #include <vecto ...