leetcode: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.
题意给两个字符串表示的数字,计算他们的乘积。
其实就是手写一个大数乘法,先翻转字符串便于从低位开始计算。
模拟乘法的运算过程,把中间结果存在data中,最后在考虑data的进位并存到结果字符串里。
注意点的就是考虑结果的前置0不要添加进去。
int data[100000];
class Solution {
public:
string multiply(string num1, string num2) {
reverse(num1.begin(),num1.end());
reverse(num2.begin(),num2.end());
memset(data,0,sizeof(data));
int len1=num1.length();
int len2=num2.length();
int i,j;
for(i=0;i<len1;++i)for(j=0;j<len2;++j)
{
data[j+i]+=(num1[i]-'0')*(num2[j]-'0');
}
int p,temp;
i=p=0;
while(i<len1+len2-1||p!=0)
{
temp=data[i]+p;
data[i]=temp%10;
p=temp/10;
++i;
}
string result;
bool flag=0;
for(;i>=0;--i)
{
if(flag==0&&data[i]==0)continue;
else
{
flag=1;
result+=(char)(data[i]+'0');
}
}
if(flag==0)return "0";
else return result;
}
};
leetcode:Multiply Strings(字符串的乘法)【面试算法题】的更多相关文章
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [leetcode]Multiply Strings @ Python
原题地址:https://oj.leetcode.com/problems/multiply-strings/ 题意: Given two numbers represented as strings ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- LeetCode: Multiply Strings 解题报告
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...
- Multiply Strings 字符串相乘
http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/4805561 ...
- python经典面试算法题1.3:如何计算两个单链表所代表的数之和
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [华为笔试题] 难度系数:⭐⭐⭐ ...
- python经典面试算法题1.4:如何对链表进行重新排序
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.4 对链表按照如下要求重新排序 [微软笔试题] 难度系数: ...
- python经典面试算法题1.2:如何从无序链表中移除重复项
本题目摘自<Python程序员面试算法宝典>,我会每天做一道这本书上的题目,并分享出来,统一放在我博客内,收集在一个分类中. 1.2 如何实现链表的逆序 [蚂蚁金服面试题] 难度系数:⭐⭐ ...
随机推荐
- AngularJS和ReactJS对比
Angular的特点: 优势: AngularJS是一套完整的框架,angular有自带的数据绑定.render渲染.angularUI库,过滤器,$filter,$directive(模板),$se ...
- 手势触摸定位(UIPanGestureRecognizer)
/** 1.相对于父坐标系而言,表示当前触摸点所在的位置 */ CGPoint locationPoint = [panGestureRecognizer locationInView:panGest ...
- Android较低版本(<5.2) 页面默认Select选择框效果的BUG解决
Bug描述: 使用低版本安卓(<5.2),在微信上打开网页,点击下拉框,会出现如下图所示的用来展示select选项的弹出框: 在选项较少的时候,可以向下滑动,将选项滑到底部 滑动前: 滑动后: ...
- CodeForces 242E - XOR on Segment 二维线段树?
今天练习赛的题....又是线段树的变换..拿到题我就敲了个点更新区间查询的..果断超时...然后想到了可以将每个数与合表示成不进位的二进制数..这样就可以区间进行更新了..比赛的时候写搓了..刚重写了 ...
- JAVA学习篇--javaweb之Filter具体解释
在DRP项目中,多次提到了Filter,它攻克了字符集的统一设置以及统一控制简单WebCache,从中我们能够体会到.它给我们带来的优点不不过降低代码量这么简单,它的出现避免了我们每一个页面反复的编写 ...
- 标准爬虫初探,来自Python之父的大餐!
首先不得不承认自己做了标题党.本文实质是分析500lines or less的crawlproject,这个project的地址是https://github.com/aosabook/500line ...
- eclipse打包 jar文件,中文乱码解决方案
直接通过eclipse浏览源代码时,发现中文注释为乱码的问题.其实这个eclipse默认编码造成的问题.可以通过以下方法解决: 修改Eclipse中文本文件的默认编码:windows->Pref ...
- 【转】asp.net 利用Global.asax 捕获整个解决方案中的异常错误
之前做项目的时候都是在每个页面中处理这不同的异常信息,一个页面数下来,很多个try{}catch{}语句块,令整个代码结构有些不够美观. 今天看到一篇帖子,是关于利用全局应用程序类来帮忙获取异常信息, ...
- 使用VS Code开发Angular 2应用程序所需配置文件的解析
目录 package.json typings.json tsconfig.json launch.json settings.json tasks.json package.json: 这是项目的基 ...
- (转) C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...