LeetCode OJ--Multiply Strings **
https://oj.leetcode.com/problems/multiply-strings/
用字符串实现大数乘法,细节题,细节很多
class Solution {
public:
string multiply(string num1, string num2) {
if(num1.empty() || num1.size() == || num2.empty() || num2.size() == )
return "";
//
if(num1 == "" || num2 == "")
return "";
//keep num2 the smaller
if(num1.size() < num2.size())
{
//exchange
string t = num1; num1 = num2; num2 = t;
}
string ans = "";
ans.resize(num1.size());
//initialize
for(size_t i = ; i < num1.size(); i++)
ans[i] = '';
int pos = ;
int times = ;
int sum = ;
for(int index2 = num2.size() - ; index2 >= ; index2--)
{
pos = ans.size() - - times;
times++;
int carry = ;
for(int index1 = num1.size() - ; index1 >=; index1--)
{
sum = carry + (num1[index1] - '')*(num2[index2] - '') + ans[pos] - '';
carry = sum/;
ans[pos] = sum% + '';
pos--;
// ans has been the beginning while index1 not, and ans has been the beginning and here are carry
if(pos == - &&(carry > || index1 != ))
{
string t = "";
t[] = carry + '';
ans = t + ans;
carry = ;
pos = ;
}
}
}
return ans;
}
};
LeetCode OJ--Multiply Strings **的更多相关文章
- 【leetcode】Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- leetcode:Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- LeetCode(43. Multiply Strings)
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 【leetcode】Multiply Strings(middle)
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- Java [Leetcode 43]Multiply Strings
题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...
随机推荐
- Android中级教程之Android应用程序的生命周期
Android应用程序的生命周期图 在大部分情况下,每个Android应用都将运行在自己的Linux进程中.当这个应用的某些代码需要执行时,进程就会被创建,并且将保持运行,直到该进程不再需要,而系统需 ...
- 精通SpringBoot--整合druid监控SQL执行
咳咳,今天我们做些简单而实用的东西,使用springboot 整合alibaba的driud数据库监控工具.alibaba已经提供了spring-boot-starter的jar包了.先看pom.xm ...
- 删除项目开发中的.pyc文件
在实际开发中python会自动生成很多pyc文件,但是这些pyc文件是不需要我们追踪的,删除了对项目也没有影响,下面是删除pyc文件的方法. Linux或Mac系统 find /tmp -name & ...
- [Noip2016]换教室(期望+DP)
Description 题目链接:Luogu Solution 这题结合了DP和概率与期望,其实只要稍微知道什么是期望就可以了, 状态的构造很关键,\(F[i][j][0/1]\)表示已经到第\(i\ ...
- jmeter XML格式的结果中各属性的含义
最近在搞jmeter,生成xml的测试报告,对报告字段进行解释,可能是自己不会找,网上资源不多,好不容易找到的,记录下来: 感谢博主:http://blog.163.com/zhang_jing/bl ...
- C# Redis存Session Hash存对象
1.新建一个控制台程序,并新建一个类“UserInfo” 2.从github下载redis的windows服务 https://github.com/ServiceStack/redis-window ...
- 数据预处理之独热编码(One-Hot Encoding)
问题的由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑以下三个特征: ["male","female"] ["from ...
- Eclipse安装MAT插件
MAT(Memory Analyzer Tool) 是基于heap dumps来进行分析的,它的分析速度比jhat快,分析结果是图形界面显示,比java内置jhat的可读性更高 通过Eclipse市场 ...
- Visual Studio 在 C# 项目添加动态链接库 dll
DLL(Dynamic Link Library)文件为动态链接库文件,又称“应用程序拓展”,是软件文件类型. 使用DLL文件的好处是程序不需要在运行之初加载所有代码,只有在程序需要某个函数的时候才从 ...
- Codeforces 1139D(期望dp)
题意是模拟一个循环,一开始有一个空序列,之后每次循环: 1.从1到m中随机选出一个数字添加进去,每个数字被选的概率相同. 2.检查这个序列的gcd是否为1,如果为1则停止,若否则重复1操作直至gcd为 ...