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 ...
随机推荐
- 排序算法—快速排序(Quick Sort)
快速排序(Quick Sort) 快速排序的基本思想:通过一趟排序将待排记录分隔成独立的两部分,其中一部分记录的关键字均比另一部分的关键字小,则可分别对这两部分记录继续进行排序,以达到整个序列有序. ...
- 一年前,我来到国企搞IT
2020.11.01日,这一天是我加盟xxx国企的一年整,这篇分享本来是要提前写的,不过由于前段时间确实繁忙,一直没有机会提笔.今天简单和大家分享下我在国企的一些工作内容,感悟等等,希望能给那些对 ...
- GROUP BY 分组后得到最新即时间最大的一条数据(需添加limit才可生效)
当使用GROUP BY 分组,默认返回的数据是组中最小的记录即id最小的数据, 当开发中经常会需要分组后将最新的数据放在前面, 为了实现需求,使用了嵌套查询,分别使用order by来排序 SELEC ...
- python爬虫04 Requests
接下来我们要来玩一个新的库 这个库的名称叫做 Requests 这个库比我们上次说的 urllib 可是要牛逼一丢丢的 毕竟 Requests 是在 urllib 的基础上搞出来的 通过它我们可以用更 ...
- 9 HTTP和HTTPS
9 HTTP和HTTPS 状态码 定义 1xx 报告 接收到请求,继续进程 2xx 成功 步骤成功接收,被理解,并被接受 3xx 重定向 为了完成请求,必须采取进一步措施 4xx 客户端出错 请求包括 ...
- 面试官问我redis数据类型,我回答了8种
面试官:小明呀,redis 有几种数据结构呀? 小明:8 种 面试官:那你说一下分别是什么? 小明:raw,int,ht,zipmap,linkedlist,ziplist,intset,skipli ...
- 我发现了一个特别Man的Linux工具!!!
Linux命令不用我多说吧,谁还不会几个?但是一个命令可能有几十种用法,就拿最简单也是最常用的ls来举例,它就有将近20种options用法 比如 ls -a :现实所有文件及其隐藏文件 ls -t ...
- nginx&http 第三章 ngx 事件event accept epoll /init
tcp 三次握手成功后,listen fd 可读,在process_event_timer 中调用rev->handler(rev)处理: 其回调函数为: ngx_event_accept / ...
- rbd-mirror新功能
RBD 的 mirroring 功能将会在下一个稳定版本Jewel中实现,这个Jewel版本已经发布了第一个版本10.1.0,这个功能已经在这个发布的版本中实现了 一.基本原理 我们试图解决的或者至少 ...
- mysql之事物
1.事物,在事物中的sql语句,要么全部执行成功,要么全部执行失败,不会出现一条sql执行成功了,一条sql执行失败的问题. 2.开启事物:就是关闭mysql自己的自动提交事物的方式 3.commit ...