题目要求: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. debian 安裝SSH 增加新用戶 并使用sudo

    1 新建新用戶user 2 3 adduser user 4 5 passwd 123654 6 7 exit 刚安装好的Debian默认还没有sudo功能. 1.安装sudo # apt-get i ...

  2. 【Android Studio】安卓开发初体验1——安装与试用

    安装 (安装预留硬盘(C盘)空余空间16G以上,8G以上内存) Intel用户: 安装Android Studio和AVD(安卓虚拟机 Android Virtual Device) 安装过程需要注意 ...

  3. n阶行列式的全排列求解(Java)

    上一个随笔,我介绍了全排列的递归求解,其中还有排列的逆序数等代码,这次我来介绍如何使用全排列计算行列式的值. 使用全排列求行列式的值,简单的描述就是: 对这个行列式每一行选取一个数,这些数处于行列式的 ...

  4. 内网安装python第三方包

    内网快速安装python第三方包 内网安装包是一个很麻烦的问题,很多时候,内网的源会出现问题,导致无法安装. 这里给出一种快速在内网中安装第三方包,无需使用内网的源. 外网操作 1.根据开发环境下的所 ...

  5. 09线程隔离的g对象

    1,g是global的意思. g对象再一次请求中的所有的代码的地方,都是可以使用的. 同一次请求,那么在这个项目的所有地方都可以用了. from flask import Flask,request, ...

  6. shell 之 -f -z -e -o -a -d

    # -f 文件存在 if[ -f /etc/sysconfig/network ]; then ./etc/sysconfig/network fi # -z 字串为空/ -o or / -a and ...

  7. 8.java设计模式之装饰者模式

    基本需求: 咖啡的种类有很多种,调料也有很多种,下单时,可以点单品咖啡也可以点单品咖啡+调料的组合,并计算下单时花费的金额 传统方式: 方式一 创建一个抽象类Drink,让所有的单品咖啡和组合咖啡都继 ...

  8. linux中/etc/passwd和/etc/shadow文件说明

    /etc/passwd是用来存储登陆用户信息: [root@localhost test]# cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x ...

  9. 创建一个自定义名称的Ceph集群

    前言 这里有个条件,系统环境是Centos 7 ,Ceph 的版本为Jewel版本,因为这个组合下是由systemctl来进行服务控制的,所以需要做稍微的改动即可实现 准备工作 部署mon的时候需要修 ...

  10. Qt For MacOs环境搭建

    使用VMWARE关于macos镜像搭建,参考https://blog.csdn.net/u011415782/article/details/78505422 关于darwin8.5.5 来安装vmt ...