leetcode:Multiply Strings(字符串的乘法)【面试算法题】
来源:互联网 发布:大数据盈利模式 编辑:程序博客网 时间:2024/06/09 18:59
题目:
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-------43. Multiply Strings(字符串的乘法运算)
- LeetCode-Multiply Strings实现大数的乘法
- Multiply Strings 两个字符串的乘法
- LeetCode 43. Multiply Strings(乘法)
- LeetCode 43 Multiply Strings(高精度乘法)
- leetcode---multiply-strings---字符串
- leetcode Multiply Strings 字符串的乘积问题
- LeetCode | Multiply Strings(字符串相乘)
- LeetCode OJ算法题(四十二):Multiply Strings
- LeetCode算法题之Multiply Strings
- 大数的乘法 Multiply Strings
- [LeetCode] Multiply Strings 字符串相乘
- LeetCode Multiply Strings 高精度乘法C++实现
- LeetCode Multiply Strings 高精度乘法C++实现
- [leetcode] 43 Multiply Strings(模拟大数乘法)
- leetcode---Multiply Strings---大整数乘法
- LeetCode | 43. Multiply Strings(大整数乘法)
- linux vsftpd 创建虚拟用户 过程记录
- 社会计算实例:10秒钟验证码能干什么?
- hibernate进行继承映射时的异常Unknown column 'allowpost' in 'field list'
- 泌尿结石要如何预防
- 学习笔记:点击网页的QQ超链接,马上弹出QQ聊天
- leetcode:Multiply Strings(字符串的乘法)【面试算法题】
- hibernate继承映射[映射单独一个tablexml版本]实例【与19相同,方式不同,未验证】(二十)
- boost thread pool
- 概述 - Node.js API 文档
- tcpdump批处理命令获取root权限的方法总结
- Jquery Datepicker操作案例
- 转:Python2入门教程 超详细1小时学会Python
- android反编译语句实例分析
- Configure Secure FTP, with VSFTPD