作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/multiply-strings/description/

题目描述

Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.

Example 1:

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2:

Input: num1 = "123", num2 = "456"
Output: "56088"

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

题目大意

实现字符串表示的数字乘法。

解题方法

先抖个机灵:

class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
return str(int(num1) * int(num2))

上面的能过,下面正经点。

此题是让我们模拟乘法,所以计算方法也就是模拟了小学数学的列竖式。从末尾数字开始计算乘积,注意进位,先得到num2中每个数字与num1的乘积,再通过10的多少次方的形式代表其位数。啊,描述起来太难了,可以想想竖式怎么列的。

另外,这个题用python这么做是不合理的,因为Python的int可以无限大的,所以没有真正实现了大数乘法。

class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
if num1 == '0' or num2 == '0':
return '0'
ans = 0
for i, n1 in enumerate(num2[::-1]):
pre = 0
curr = 0
for j, n2 in enumerate(num1[::-1]):
multi = (ord(n1) - ord('0')) * (ord(n2) - ord('0'))
first, second = multi // 10, multi % 10
curr += (second + pre) * (10 ** j)
pre = first
curr += pre * (10 ** len(num1))
ans += curr * (10 ** i)
return str(ans)

根据JustDoIT的做法,首先我们把每一位相乘,得到一个没有进位的临时结果,如图中中间的一行红色数字就是临时结果,然后把临时结果从低位起依次进位。对于一个m位整数乘以n位整数的结果,最多只有m+n位。

C++代码如下:

class Solution {
public:
string multiply(string num1, string num2) {
const int M = num1.size();
const int N = num2.size();
const int k = M + N - 2;
vector<int> res(M + N, 0);
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
res[k - i - j] += (num1[i] - '0') * (num2[j] - '0');
}
}
int carry = 0;
for (int i = 0; i < res.size(); ++i) {
res[i] += carry;
carry = res[i] / 10;
res[i] %= 10;
}
int start = res.size() - 1;
while (start >= 0 && res[start] == 0)
--start;
if (start < 0) return "0";
string ans;
while (start >= 0) {
ans += char(res[start] + '0');
--start;
}
return ans;
}
};

日期

2018 年 6 月 13 日 —— 腾讯赛圆满结束!两个月修得正果哈哈~~
2019 年 3 月 3 日 —— 3月开始,春天到了

【LeetCode】43. Multiply Strings 解题报告(Python & C++)的更多相关文章

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

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

  2. LeetCode: Multiply Strings 解题报告

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

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

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

  4. Java [Leetcode 43]Multiply Strings

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

  5. LeetCode(43. Multiply Strings)

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

  6. [leetcode]43. Multiply Strings高精度乘法

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

  7. LeetCode 43 Multiply Strings(字符串相乘)

    题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description     求解大数相乘问题   按照上图所示,进行嵌套循环计算 ...

  8. leetcode 43. Multiply Strings(高精度乘法)

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

  9. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

随机推荐

  1. Oracle--计算某一日期为一年中的第几周

    我自己实现的脚本: select T31267.CREATED_DATE as F31265, (select to_char(to_date(T31267.CREATED_DATE,'yyyy-mm ...

  2. mysql 不等于 符号写法

    今天在写sql语句的时候,想确认下mysql的不等于运算符是用什么符号表示的 经过测试发现mysql中用<>与!=都是可以的,但sqlserver中不识别!=,所以建议用<> ...

  3. Java 读取TXT文件的多种方式

    1).按行读取TXT文件package zc;import java.io.BufferedReader;import java.io.File;import java.io.FileNotFound ...

  4. 🏆【Alibaba中间件技术系列】「Sentinel技术专题」分布式系统的流量防卫兵的基本介绍(入门源码介绍)

    推荐资料 官方文档 官方demo Sentinel 是什么? 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentinel 以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度保护 ...

  5. day01互联网架构理论

  6. C语言大小端判定

    要判定大小端?需要弄清以下几个问题: 1.当一个变量占多个字节时,变量的指针指向的是低地址 2.什么是大小端? 大端模式:是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中. 小 ...

  7. VSCode+Maven+Hadoop开发环境搭建

    在Maven插件的帮助下,VSCode写Java其实非常方便.这一讲我们介绍如何借助maven用VScode搭建Hadoop开发环境. 1.Java环境安装 首先我们需要搭建好Java开发环境.我们需 ...

  8. Gradle插件详解

    参考[1]Gradle 插件       [2]修改 Gradle 插件(Plugins)的下载地址(repositories)

  9. request.getRequestDispatcher()和response.sendRedirect()区别

    一.request.getRequestDispatcher().forward(request,response): 1.属于转发,也是服务器跳转,相当于方法调用,在执行当前文件的过程中转向执行目标 ...

  10. numpy基础教程--clip函数的使用

    在numpy中,clip函数的原型为clip(self, min=None, max=None, out=None),意思是把小于min的数全部置换为min,大于max的数全部置换为max,在[min ...