LeetCode 43. Multiply Strings

简介: 给定两个表示为字符串形式的非负整数num1和num2,返回num1和num2的乘积,也表示为字符串形式。

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:

The length of both num1 and num2 is < 110.

Both num1 and num2 contain only digits 0-9.


Both num1 and num2 do not contain any leading zero, except the number 0 itself.

You must not use any built-in BigInteger library or convert the inputs to integer directly.


描述



给定两个表示为字符串形式的非负整数num1和num2,返回num1和num2的乘积,也表示为字符串形式。


例:输入 num1 = "2", num2 = "3",输出 "6" 输入 num1 = "123",num2 = "456", 输出"56088"


注意:


  • 字符串num1和num2的长度都小于110
  • 字符串num1和num2都只包含数字0-9
  • 字符串num1和num2起始都不会为0,除了num1和num2只有一个字符0本身的情况
  • 不能使用任何函数自带得乘法运算也不能使用字符串转换为整数的函数


思路



回顾乘法的运算法则,如下图


v2-506174a5b5bb8133f9d9249e24e0a125_720w.jpg


  • 如上图所示,我们把两个数右对齐,依次排好,从右至左依次进行运算,每次运算结果按位置对其,最后进行加和,这就完成了乘法运算
  • 再给出一个示例,如下图


v2-e679b6eb3337f8434a7f57e15a6fb67b_720w.jpg


class Solution:
    def multiply(self, num1, num2):
        """
        :type num1: str
        :type num2: str
        :rtype: str
        """
        if num1 == '0' or num2 == '0':
            return '0'
        lennum1, lennum2 = len(num1), len(num2)
        result = [0]*(lennum1+lennum2)
        # 共有两层
        for i in range(lennum1-1, -1, -1):
            for j in range(lennum2-1, -1, -1):
                # 首先计算乘积,注意用ASCII码的方式转换为数值
                temp = (ord(num1[i])-ord('0'))*(ord(num2[j])-ord('0'))
                # 计算所得应和i+j位置求和
                _sum = temp+result[i+j+1]
                # 第i+j个位置
                result[i+j+1] = _sum % 10
                # 第i+j-1个位置,此代码为python3,注意使用'//'等号进行运算,例5/2=2.5,5//2=2
                result[i+j] += _sum//10
        # 将数字转换为字符串
        result = [str(i) for i in result]
        return ''.join(result[1:]) if result[0] == "0" else ''.join(result)


源文件地址在这里

目录
相关文章
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
81 0
LeetCode 415. Add Strings
LeetCode 205. Isomorphic Strings
给定两个字符串 s 和 t,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
63 0
LeetCode 205. Isomorphic Strings
|
人工智能 算法
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
LeetCode 1347. 制造字母异位词的最小步骤数 Minimum Number of Steps to Make Two Strings Anagram
|
Java 索引 Python
LeetCode 205:同构字符串 Isomorphic Strings
题目: 给定两个字符串 s 和 *t*,判断它们是否是同构的。 如果 s 中的字符可以被替换得到 *t* ,那么这两个字符串是同构的。 所有出现的字符都必须用另一个字符替换,同时保留字符的顺序。两个字符不能映射到同一个字符上,但字符可以映射自己本身。
883 0
leetcode 205. Isomorphic Strings
题目 Given two strings s and t, determine if they are isomorphic.
686 0
LeetCode - 43. Multiply Strings
43. Multiply Strings  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定两个字符串,计算这两个字符串相乘的结果.
914 0
|
索引 Java
LeetCode 205 Isomorphic Strings(同构的字符串)(string、vector、map)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50611168 翻译 给定两个字符串s和t,决定它们是否是同构的。
837 0
|
12天前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
25 6