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. 前端路由以及浏览器回退,hash & history & location

    一.前言 其实不止一次想监听浏览器的回退方法,比如 在 list.html 页滚动加载了几页列表,点到 detail.html 看详情,反回来时又得重新加载几页 H5 有背景音乐的,跳页就得重新放,体 ...

  2. C++设计模式 之 “接口隔离” 模式:Facade、Proxy、Mediator、Adapter

    “接口隔离”模式 在组建构建过程中,某些接口之间之间的依赖常常会带来很多问题.甚至根本无法实现.采用添加一层间接(稳定)接口,来隔离本来相互紧密关联的接口是一种常见的解决方案. 典型模式 #Facad ...

  3. 自定义LisetView

    1.ListView listview=findViewById(R.id.listview); 2.public class MyAdapter extends BaseAdapter{ priva ...

  4. Metasploit应用举例

    本篇文章包含以下几方面内容: 1.Metasploit端口扫描: 2.用其他模块 3.metasploit smb获取系统信息 4.Metsploit服务识别 5.ftp识别: 6.metasploi ...

  5. Java基础部分二

    1.&与&& &位运算符,&&逻辑与运算符&&还具有短路的功能,即如果第一个表达式为false,则不再计算第二个表达式 2.switch ...

  6. 使用PDFminer3k解析pdf为文字遇到:WARING:root:GBK-EUC-H

    最近需要把PDF解析为文字,查了查python的模块,发现PDFminer3k能满足需求.我使用的是 windows平台下的python3.6,python2的则下载pdfminer. 首先下载:直接 ...

  7. 实现简单的ORM

    介绍 本篇将介绍实现简单的ORM,即:对数据表的通用操作:增.删.改.查 数据访问层 数据访问层类图 类说明: 1.DbProvider(供应):为数据操作提供基本对象,如:连接.操作对象.事务... ...

  8. python-ConfigParser模块--转载

    1,函数介绍 1.1.读取配置文件 -read(filename) 直接读取ini文件内容-sections() 得到所有的section,并以列表的形式返回-options(section) 得到该 ...

  9. nodejs+gulpjs压缩js代码

    1.安装node.js 下载地址:nodejs.org  或者  nodejs.cn 2.安装gulp之前我们需要安装nodejs的环境,检测能够正常使用npm后,我们用npm对gulp进行一次全局安 ...

  10. go 并发 demo

    两个进程执行两个goroutine // This sample program demonstrates how to create goroutines and // how the schedu ...