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 ...
随机推荐
- c++中对应java ShutdownHook的退出处理器
最近学习cpp(至于为什么,可参考http://www.cnblogs.com/zhjh256/p/6321972.html),c++标准库第二版5.8.2节的时候,发现c++有一个对应java Sh ...
- Total Difference String
Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视 ...
- 05: api认证
1.1 api认证原理介绍 1.api认证原理:客户端生成秘钥 1) 客户端与服务器端有一个相同的字符串作为auth_key 2) 客户端使用encryption="auth_key|tim ...
- CodeForces 76A Gift - 最小生成树
The kingdom of Olympia consists of N cities and M bidirectional roads. Each road connects exactly tw ...
- Online Judge 2014 K-th Number -主席树
You are working for Macrohard company in data structures department. After failing your previous tas ...
- linux下安装微信小程序开发工具
一.环境:: ubuntu 16.04 二.安装过程: 2.1 安装wine sudo apt-get install wine 2.2 安装nwjs-sdk 2.2.1 下载linux版nwjs-s ...
- Luogu P1314 聪明的质监员 二分答案
题目链接 Solution 这个范围不是二分就是结论题就是数学题... 然后再看一会差不多就可以看出来有单调性所以就可以确定二分的解法了 二分那个$W$,用前缀和$O(n+m)$的时间来求出对答案的贡 ...
- Pyinstaller 中 pandas出错问题的解决(详细)
环境配置 pip install pyinstaller pyinstaller中的参数 -F 表示生成单个可执行文件 -c 显示命令行窗口,一般一开始的时候使用,如果没有错误,那就使用-w命令 -w ...
- Ubuntu 14.04 安装libssh
参考: libssh [CMake] include command Ubuntu 14.04 安装libssh $ git clone https://github.com/substack/lib ...
- UVa 674 Coin Change(完全背包)
https://vjudge.net/problem/UVA-674 题意: 计算兑换零钱的方法共有几种. 思路: 完全背包基础题. #include<iostream> #include ...