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 如何实现链表的逆序 [蚂蚁金服面试题] 难度系数:⭐⭐ ...
随机推荐
- jQuery扩展与noConflict的用法-小示例
有时我们要用到自己定义的jquery,这时可以通过jQuery扩展来实现该功能 index.html <!DOCTYPE html> <html> <head> & ...
- eCharts的随笔
1.散点图中找最优记录 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
- TypedArray和obtainStyledAttributes使用
在编写Android自定义按钮示例基础上,如果要指定字体大小产生这样的效果: 其实是不需要自定义变量的,可以直接使用TextView的配置属性: <com.easymorse.textbutto ...
- cocos2d-x anchorPoint
之前一直没有用过anchorPoint,也感觉用这个东西的地方相对比较少的,都是直接使用世界坐标来定位的. 但是,在现在这个项目中,却有同事使用了这个anchorPoint,使用是使用了,但是,在碰撞 ...
- linux下查阅文件内容cat,more,less,tail
1.常用cat,直接查看,一次性全部输出 cat filename cat -b filename 显示行号,除空白行 cat -n 显示行号,包括空白行 常用:cat filename | ...
- IE浏览器下a标签嵌套img标签默认带有边框
最近写在线主页时发现IE浏览器下a标签嵌套img标签默认带有边框: 解决办法:img{border:0 none;} 注意,严格意义上0和none都要加上!
- 转载——SqlServer之like、charindex、patindex
转载自:http://www.2cto.com/database/201305/214967.html SqlServer之like.charindex.patindex 1.环境介绍 测试环境 ...
- C#Log4net日志记录组件的使用
一.Log4Net介绍 Log4net是基于.NET开发的一款非常著名的记录日志开源组件.它通过一套XML配置的日志引擎,将日志分不同的等级,分别是:FATAL . ERROR. WARN. INFO ...
- pch文件出现no such file or directory错误
一般出现这种情况是由于项目直接拷贝到其他电脑上运行... clang: error: no such file or directory: '/demo2/控件代码/13/Recorder/Recor ...
- 旧版asp.net 发送邮件代码
说到发送邮件发送,先提一下SMTP(呵呵,高手就跳过这一段吧!). SMTP的全称是“Simple Mail Transfer Protocol”,即简单邮件传输协议.它是一组用于从源地址到目的地址传 ...