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 如何实现链表的逆序 [蚂蚁金服面试题] 难度系数:⭐⭐ ...
随机推荐
- hdu 4841 圆桌问题(STL vector)
Problem Description 圆桌上围坐着2n个人.其中n个人是好人,另外n个人是坏人.如果从第一个人开始数数,数到第m个人,则立即处死该人:然后从被处死的人之后开始数数,再将数到的第m个人 ...
- (ubuntu)在andorid andk工程中使用ccache加速编译速度
环境 系统:Linux luogw-pc 3.5.0-36-generic #57~precise1-Ubuntu SMP Thu Jun 20 18:21:09 UTC 2013 x86_64 x8 ...
- Intel 被 ARM 逼急了
英特尔最近推出基于Silvermont架构Bay Trail系列处理器,相对前一代Bonnell架构的最突出的改进就是支持乱序执行 silvermon架构的处理器将出现在pc,平板等: List of ...
- 玩转Win32开发(2):完整的开发流程
上一篇中我给各位说了一般人认为C++中较为难的东西——指针.其实对于C++,难点当然不局限在指针这玩意儿上,还有一些有趣的概念,如模板类.虚基类.纯虚函数等,这些都是概念性的东西,几乎每一 ...
- 只响应ccTouchBegan的问题
在Touch事件中,ccTouchBegan有一个返回值,而这一个返回值则决定了是否会继续响应ccTouchMoved,ccTouchEnded. 如果没有返回true的话,则直接会结束此Touch事 ...
- 判断http 请求来自于手机还是PC
首先收集了部分客户端请求头部信息如下 iPhone微信: User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_1_2 like Mac OS X) App ...
- .net打包/c#winfrom程序打包
1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点击确定.(详细见下图) 此主题相关图片如下: 2:安装向导 关闭后打开安 ...
- FileDirLocationOperator - 文件或目录位置操作.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Move ...
- ORACLE查询数据库的锁表情况
查询数据库的锁表情况语句如下: SELECT p.spid,a.serial#, c.object_name,b.session_id,b.oracle_username,b.os_user_na ...
- [Leetcode][021] Merge Two Sorted Lists (Java)
题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 ...