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 ...
随机推荐
- Java学习的第二十四天
1. 目录管理 2.文件方法太多记不清 3.明天学习流和流的分类
- numpy基础读写
一.npy,npz 格式的读写 1.写入 a.Savez("存储地址",数组1,数组2,-,数组n) --npz,将多个数组保存到文件 b.Save("存储地址" ...
- Kafka_2.12-2.5.1集群搭建与参数调优
Kafka是目前业界使用最广泛的消息队列.数据流转常见这样的业务场景,客户端把采集到的日志推送给Kafka,业务方可以消费Kafka的数据落地HDFS,用于离线分析,也可以使用Spark或Flink消 ...
- Python爬虫简单实现CSDN博客文章标题列表
Python爬虫简单实现CSDN博客文章标题列表 操作步骤: 分析接口,怎么获取数据? 模拟接口,尝试提取数据 封装接口函数,实现函数调用. 1.分析接口 打开Chrome浏览器,开启开发者工具(F1 ...
- WSL Ubuntu 18.04 LTS + VS Code
WSL Ubuntu 18.04 LTS + VS Code WSL(Windows Subsystem for Linux)使得Windows用户能够在Windows系统上使用原生的Linux环 ...
- C# 集合类(三)
C# 集合类自己经常用到: 数组(Array).动态数组(ArrayList).列表(List).哈希表(Hashtable).字典(Dictionary),对于经常使用的这些数据结构,做一个总结,便 ...
- leetcode131:letter-combinations-of-a-phone-number
题目描述 给出一个仅包含数字的字符串,给出所有可能的字母组合. 数字到字母的映射方式如下:(就像电话上数字和字母的映射一样) Input:Digit string "23"Outp ...
- .Net 5 正式版RTM 发布
下载连接 https://dotnetcli.azureedge.net/dotnet/Sdk/5.0.100-rtm.20515.8/dotnet-sdk-5.0.100-rtm.20515.8-w ...
- intelx86为何从0xFFFF0处执行
第一条指令的地址 在用户按下计算机电源开关之后,CPU会自动的将其CS寄存器设定为0xFFFF,将其IP寄存器设定为0x0000.由于CS:IP指出了下一条指令的地址[1],因此CPU会跳到0xFFF ...
- phpstudy2016-2018漏洞验证
影响版本 漏洞验证 查看目录下 php_xmlrpc.dll PHPTutorial\php\php-5.4.45\ext\php_xmlrpc.dll存在@eval(%s('%s'));即说明有后门 ...