43. 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.
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 字符串表示的大数乘法的更多相关文章
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- 43. Multiply Strings 字符串相乘
1. 原始题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2&qu ...
- Leetcode43. Multiply Strings字符串相乘(大数相乘)
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = "2", num ...
- 43. Multiply Strings字符串相乘
网址:https://leetcode.com/problems/multiply-strings/submissions/ 参考:https://leetcode.com/problems/mult ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- Core Java 2
p267~p270: 1.一个方法不仅需要告诉编译器将要返回什么值, 还要告诉编译器有可能发生什么错误(以便在错误发生时用妥善的方式处理错误). 2.方法应该在首部声明所有可能抛出的异常. 3.方法抛 ...
- 负载均衡之-LVS
负载均衡用的很多,这里对负载均衡做一个总结吧,总共包含下面几片博文. LVS负载均衡 keepalived负载均衡+高可用 haproxy负载均衡 nginx负载均衡 LVS负载均衡 LVS是章文嵩博 ...
- python的time时间模块
模块概述 1.一个.py文件就是一个模块 2.通过import语句在一个模块中导入另一个模块,import sys,print (sys.path),sys.path的结果为一个列表,列表的第一个元素 ...
- Android http通信案例
Android studio 编写安卓程序,实现 http 通信,获得百度主页源代码. -------------------------------------------------------- ...
- 牛客网试卷: 京东2019校招笔试Java开发工程师笔试题(1-)
1.在软件开发过程中,我们可以采用不同的过程模型,下列有关 增量模型描述正确的是() A 是一种线性开发模型,具有不可回溯性 B 把待开发的软件系统模块化,将每个模块作为一个增量组件,从而分批次地分析 ...
- 第八章 对称加密算法--AES
注意:本节内容主要参考自<Java加密与解密的艺术(第2版)>第7章“初等加密算法--对称加密算法” 8.1.AES 特点: 密钥建立时间短.灵敏性好.内存需求低(不管怎样,反正就是好) ...
- github上不去了
这几天发现github.com上不去了 可能是由于要杜绝国外的人使用最新的技术??从而屏蔽了吗?
- [Pytorch]Pytorch中tensor常用语法
原文地址:https://zhuanlan.zhihu.com/p/31494491 上次我总结了在PyTorch中建立随机数Tensor的多种方法的区别. 这次我把常用的Tensor的数学运算总结到 ...
- org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected subelement profile
原创:转载请注明出处 1.异常情况 org.apache.axis2.AxisFault: org.apache.axis2.databinding.ADBException: Unexpected ...
- UVa 1411 Ants(分治)
https://vjudge.net/problem/UVA-1411 题意:n只蚂蚁和n颗苹果树,一一配对并且不能交叉. 思路:这就是巨人与鬼的问题.用分治法就行了. #include<ios ...