题目要求: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的更多相关文章

  1. Java for LeetCode 043 Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  2. 【leetcode】Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  3. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  4. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  5. LeetCode(43. Multiply Strings)

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

  6. 【leetcode】Multiply Strings(middle)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. leetcode:Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  8. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  9. leetcode:Multiply Strings(字符串的乘法)【面试算法题】

    题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note ...

随机推荐

  1. Java学习的第八天

    1.求平均值 冒泡法 选择排序 插入排序 快速排序 二分法查找 使用工具排序 工具二分法查找 生成随机数 2.在二分法时,没有强调要先排序才可以二分法.递归没太看明白. 3.明天学习综合实例和第四章开 ...

  2. ModelSerializer 高级使用

    前言 ModelSerializer中还具有一些高级用法,如批量更新.批量删除.批量创建等. 但是批量过来的数据格式都需要与前端做好协商,什么样的数据格式是单条操作,什么样的数据格式是批量操作. 如下 ...

  3. C语言经典100例-ex001

    系列文章<C语言经典100例>持续创作中,欢迎大家的关注和支持. 喜欢的同学记得点赞.转发.收藏哦- 后续C语言经典100例将会以pdf和代码的形式发放到公众号 欢迎关注:计算广告生态 即 ...

  4. 转载-Eclipse导入第三方库的方法

    作者:wyf_phper 原文:https://blog.csdn.net/qq_32985981/article/details/49976193 一:导入*.jar包步骤:将下载好的jar包复制到 ...

  5. 手写Koa.js源码

    用Node.js写一个web服务器,我前面已经写过两篇文章了: 第一篇是不使用任何框架也能搭建一个web服务器,主要是熟悉Node.js原生API的使用:使用Node.js原生API写一个web服务器 ...

  6. 一个 Task 不够,又来一个 ValueTask ,真的学懵了!

    一:背景 1. 讲故事 前几天在项目中用 MemoryStream 的时候意外发现 ReadAsync 方法多了一个返回 ValueTask 的重载,真是日了狗了,一个 Task 已经够学了,又来一个 ...

  7. Android Google官方文档解析之——System Permissions

    Android is a privilege-separated operating system, in which each application runs with a distinct sy ...

  8. 10 XSRF和XSS

    10 XSRF和XSS CSRF(Cross-site request forgery)跨站请求伪造 XSS(Cross Site Scripting)跨站脚本攻击 CSRF重点在请求,XSS重点在脚 ...

  9. 【python】将变量保存在本地及读取

    在用jupyter notebook写python代码的过程中会产生很多变量,而关闭后或者restart jupyter kernel后所有变量均会消失,想要查看变量就必须将代码重新再运行一遍,而想在 ...

  10. 针对DEV XtraReport中没有radiobuttonlist的替代方法

     private void PrintingSystem_EditingFieldChanged(object sender, DevExpress.XtraPrinting.EditingField ...