43. Multiply Strings 字符串相乘

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

Note:

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

class Solution1:

    def multiply(self, num1, num2):

        """

        :type num1: str

        :type num2: str

        :rtype: str

        """

        if num1 is "0" or num2 is "0":

            return "0"

        pos = [[] for i in range(len(num1) + len(num2))]

        startIndex = 0

        for i in range(len(num2) - 1, -1, -1):

            curStartIndex = 0

            for j in range(len(num1) - 1, -1, -1):

                cur = str(int(num2[i]) * int(num1[j]))

                curIndex = 0

                for k in range(len(cur) - 1, -1, -1):

                    pos[startIndex + curStartIndex + curIndex].append(cur[k])

                    curIndex += 1

                curStartIndex += 1

            startIndex += 1

        res = ""

        carry = 0

        index = 0

        while index < len(pos) or carry:

            val = carry

            for cur in pos[index]:

                val += int(cur)

            carry = int(val / 10)

            res = str(val % 10) + res

            index += 1

        return res.lstrip("0")

class Solution2:

    def multiply(self, num1, num2):

        res = 0

        for i in range(len(num1)):

            res *= 10

            n = int(num1[i])

            temp_n = 0

            for j in range(len(num2)):

                temp_n *= 10

                temp_n += int(num2[j]) * n

            res += temp_n

        return str(res)

s = Solution1()

num1 = "999"

num2 = "999"

res = s.multiply(num1, num2)

print(res)

来自为知笔记(Wiz)

原文地址:https://www.cnblogs.com/xiejunzhao/p/8445796.html

时间: 2024-10-07 23:58:15

43. Multiply Strings 字符串相乘的相关文章

[LeetCode] 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. 这道题让我们求两个字符串数字的相乘,输入的两个数和返回的数都是以字符串格式储存的,这样做的原因可能是这样可以计算超大数相乘,可以不受int或long的数值范围的约束,那么我们该如何来计算

43. 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. string multiply(string& num, char ch){ int n = ch - '0'; string s; int carry = 0; int x; for(

leetcode 43 Multiply Strings 大数相乘

感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(num1.charAt(0) == '0' || num2.charAt(0) == '0'){ return "0"; } int len1 = num1.length(); int len2 = num2.length(); int len = len1+len2; int[] arr =

Multiply Strings 字符串相乘

http://www.cnblogs.com/TenosDoIt/p/3735309.html https://blog.csdn.net/fly_yr/article/details/48055617 class Solution { public: string multiply(string num1, string num2) { int len1 = num1.length(); int len2 = num2.length(); vector<int> res(len1+len2,

&lt;LeetCode OJ&gt; 43. Multiply Strings

43. Multiply Strings My Submissions Question Total Accepted: 51859 Total Submissions: 231017 Difficulty: Medium 以字符串的形式给定两个数字,返回相乘的结果,注意:结果也是字符串,因为数字可能很大 Given two numbers represented as strings, return multiplication of the numbers as a string. Note

43. Multiply Strings(js)

43. Multiply Strings 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: Inp

【一天一道LeetCode】#43. Multiply Strings

一天一道LeetCode系列 (一)题目 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. Converting the input string to integer is NOT allowed. You should NOT us

Java [Leetcode 43]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. 解题思路: 设置数组记录单个位置相乘的结果,最后负责相加进位. 代码如下: public class Solution { public String multiply(St

LeetCode OJ: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. 给出两个字符串,返回对应数字想乘后的字符串,由于这个字符串可能很大,所以不能采用一般的乘法,这里用的方法是模拟手工的乘法运算,算法 本身很简单,就是当时写的时候有些很小的细节搞错了,找了