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实现经典的排序算法
排序 关注公众号"轻松学编程"了解更多. 1.冒泡排序 基本思想:比较相邻的元素大小,将小的前移,大的后移,就像水中的气泡一样,最小的元素经过几次移动,会最终浮到水面上.原地排序, ...
- Luogu P1856 [USACO5.5]矩形周长Picture
线段树+扫描线 经典的扫描线问题 首先将一个矩形看作由竖着的两条边和横着的两条边构成 那分成两次考虑,一次考虑竖边,一次考虑横边 首先考虑横边 如图两个矩形,现将横边擦去,留下竖边,将平面划分成3个区 ...
- 2020年的UWP(3)——UWP和desktop extension的简单交互
上一篇<2020年的UWP(2)--In Process App Service>中我们了解了UWP和Desktop Extension可以通过AppService进行数据交互.本篇我们就 ...
- JVM常用调优工具介绍
前言 王子在之前的JVM文章中已经大体上把一些原理性问题说清楚了,今天主要是介绍一些实际进行JVM调优工作的工具和命令,不会深入讲解,因为网上资料很多,篇幅可能不长,但都是实用的内容,小伙伴们有不清楚 ...
- 《<SPRING5高级编程(第5版)>_王净译》笔记-【目录】
第一次写这玩意,不知道什么时候能写完,今天项目比较近,期望年底能看完吧. 先定个小目标 20201228 完成 第1章 Spring介绍 第2章 入门 第3章 在Spring中引入IoC和DI 第4章 ...
- 18FlaskRESTful
一,虚拟环境下安装(win) 在安装这个环节发现了很多问题,pycharm里装了之后发现根本引用不了,查明原因是因为第三方库pycharm无法识别. 解决办法:进入虚拟环境直接pip. 二,基本使用 ...
- java-Queue方法
Collection>Queue // 1. 新增 add/ offer boolean add(E e); // 队列满,IllegalStateException boolean offer ...
- vs code远程开发
VS Code如何配置远程开发 你是如何远程开发的?还在使用FTP/SFTP同步文件?那你out了,有了宇宙第一IDE:VS就不需要这么麻烦了,一起学习一下吧. 第一步,安装Remote SSH插件 ...
- psycopg2模块安装问题
我的平台是win10(x64).python3.7,打算通过psycopg2模块来操作Greenplum数据库,我通过pip install psycopg2 安装了psycopg2模块,也提示安装成 ...
- CTF-WEB-HCTF 2018 Warmup
题目链接 攻防世界-Warmup 解题思路 [原题复现]HCTF 2018 Warmup(文件包含)