LeetCode 043 Multiply Strings
题目要求: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.
分析:
参考网址:http://blog.csdn.net/pickless/article/details/9235907
利用竖式的思想,进行乘法运算。
3 4
* 1 3
——————
9 12
3 4
——————
3 13 12
再处理进位:
3 13 12 - > 3 14 2 -> 4 4 2
代码如下:
class Solution {
public:
string multiply(string num1, string num2) {
int flag = 1;
//先处理符号
if (num1[0] == '-' || num2[0] == '-') {
if (num1[0] == '-') {
flag *= -1;
num1 = num1.substr(1, num1.size() - 1);
}
if (num2[0] == '-') {
flag *= -1;
num2 = num2.substr(1, num2.size() - 1);
}
}
int length = num1.size() + num2.size() + 1;
int s[length];
memset(s, 0, sizeof(int) * length);
int i = 0, temp = 0;
for (i = 0; i < num1.size(); i++) {
for (int j = 0; j < num2.size(); j++) {
s[(num1.size() - i - 1) + (num2.size() - j - 1)] += (num1[i] - '0') * (num2[j] - '0');
}
}
//进位
for (i = 0; i < length; i++) {
s[i + 1] += s[i] / 10;
s[i] = s[i] % 10;
}
for (i = 0; i < length / 2; i++) {
temp = s[i];
s[i] = s[length - i - 1];
s[length - i - 1] = temp;
}
string ans = flag < 0 ? "-" : "";
for (i = 0, temp = -1; i < length; i++) {
if (s[i] != 0) {
temp = 1;
}
if (temp > 0) {
ans += s[i] + '0';
}
}
return ans == "" ? "0" : ans;
}
};
LeetCode 043 Multiply Strings的更多相关文章
- 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
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [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 ...
- 【leetcode】Multiply Strings(middle)
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 ...
- 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 ...
随机推荐
- Python爬虫urllib模块
Python爬虫练习(urllib模块) 关注公众号"轻松学编程"了解更多. 1.获取百度首页数据 流程:a.设置请求地址 b.设置请求时间 c.获取响应(对响应进行解码) ''' ...
- unicode与编码的关系
参考链接先贴上来:https://blog.csdn.net/humadivinity/article/details/79403625https://www.cnblogs.com/kevin2ch ...
- 利用GitHub和Hexo打造免费的个人博客
每个程序猿都需要一个个人博客,目前广泛出现在大家视野里的有CSDN.博客园.简书,但是他们却没有给用户一个专属的站点.一个好记的域名.你需要一个https://xxx.xxx.xxx/格式的网址,一个 ...
- LOJ #6029. 「雅礼集训 2017 Day1」市场 线段树维护区间除法
题目描述 从前有一个贸易市场,在一位执政官到来之前都是非常繁荣的,自从他来了之后,发布了一系列奇怪的政令,导致贸易市场的衰落. 有 \(n\) 个商贩,从\(0 \sim n - 1\) 编号,每个商 ...
- 思科ASA放行主/被动FTP
实验环境: 设备说明: internet是一台windows10,用于模拟外网客户 ASA是思科ASA防火墙 FTP-SERVER是Centos7,Centos7上安装了vsftpd 实验说明: 本文 ...
- 《.NET 5.0 背锅案》第2集:码中的小窟窿,背后的大坑,发现重要嫌犯 EnyimMemcachedCore
在第1集的剧情中,主角是".NET 5.0 正式版 docker 镜像",它有幸入选第1位嫌疑对象,不是因为它的嫌疑最大,而是它的验证方法最简单,只需要再进行一次发布即可.我们在周 ...
- 软件工程作业--ATM自助银行服务系统
博客班级 AHPU软件工程 作业要求 ATM自助银行服务系统 作业目标 编码实现ATM自助银行服务系统 学号 3180701118 目录 一.作业要求 二.代码及运行界面 1.test类 2.User ...
- selenium之 定位以及切换frame(iframe)(转)
frame标签有frameset.frame.iframe三种,frameset跟其他普通标签没有区别,不会影响到正常的定位,而frame与iframe对selenium定位而言是一样的,seleni ...
- python之路 《六》函数
---恢复内容开始--- 为什么要有函数? 当你的老板要你写一个程序 1 def 函数0(): 2 # 如果cpu占用率>90 3 # 发送邮件 4 # 发出警报 5 6 def 函数1(): ...
- JS简单介绍与简单的基本语法
1.JavaScirpt是一门编程语言,是为前端服务的一门语言. (1)基础语法 (2)数据类型 (3)函数 (4)面向对象 2.还涉及到BOM和DOM (1)BOM(操作浏览器的一些功能) (2)D ...