[LeetCode]题解(python):043-Multiply Strings



题目来源



https://leetcode.com/problems/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.



题意分析



Input: two numbers expressed as string

Output:the multiply of the two sums

Conditions:数可以无限大,做两个数的乘法

如:"23650379502752" 和 "865382861454"

结果:"20466633088564555427721408"



题目思路



首先将两个str转化为整数的list,然后考虑到乘积的位数必然小于等于len(str1)+len(str2),先初始化乘积为0的list,然后按照位数相乘的规律去做

注意:

1 最后结果需要将大数的0去掉,同时如果结果为0需要返回串“0”

2 翻转:mul.reverse()



AC代码(Python)


 1 _author_ = "YE"
 2 # -*- coding:utf-8 -*-
 3
 4 class Solution(object):
 5     def multiply(self, num1, num2):
 6         """
 7         :type num1: str
 8         :type num2: str
 9         :rtype: str
10         """
11         len1 = len(num1)
12         len2 = len(num2)
13
14         list1 = [0 for i in range(len1)]
15         list2 = [0 for i in range(len2)]
16
17         for i in range(len1):
18             list1[len1 - 1 - i] = int(num1[i])
19         for i in range(len2):
20             list2[len2 - 1 -i] = int(num2[i])
21
22         # print(list1,list2)
23
24         mul = [0 for i in range(len1 + len2)]
25
26         for i in range(len2):
27             carry = 0
28             for j in range(len1):
29                 mul[i + j] = mul[i + j] + carry + (list2[i] * list1[j]) % 10
30
31                 carry = (list2[i] * list1[j]) // 10
32
33                 if mul[i + j] >= 10:
34                     carry = carry + mul[i + j] // 10
35                     mul[i + j] = mul[i + j] % 10
36
37
38             if carry > 0:
39                 mul[i + len1] += carry
40                 if mul[i + len1] > 10:
41                     mul[i + len1] = mul[i + len1] % 10
42                     carry += mul[i + len1] // 10
43
44         index = len1 + len2 - 1
45         while index >= 0:
46             if mul[index] > 0:
47                 break
48             index -= 1
49
50         if index + 1 < len1 + len2:
51             mul[index+1:] = []
52
53         mul.reverse()
54
55         s = ‘‘
56         for i in range(len(mul)):
57             s += str(mul[i])
58         if s == ‘‘:
59             s = ‘0‘
60         return s
61
62 str1 = ‘12312‘
63 str2 = ‘4234‘
64 s = Solution()
65
66 print(s.multiply(str1,str2))
时间: 2024-08-23 16:21:44

[LeetCode]题解(python):043-Multiply Strings的相关文章

LeetCode 043 Multiply Strings

题目要求: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 利用竖式的思想,

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

43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. LeetCode43. Multiply Strings中等 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例?2: 输入: num1 = "123", num2 = "456&q

Java for LeetCode 043 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. 解题思路一: BigInteger!!! JAVA实现如下: static public String multiply(String num1, String num2) { java

leetcode 第42题 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. 就是实现大数乘法.嘿嘿,我先用long long直接乘试试手感,试过了不行.所以还是要用字符串表示才行. 我是这样做的,先实现两个子函数,一个是实现一个数和一个字符串的数相乘的结果

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. 分析 计算两个字符串表示的非负大整数的乘积,结果仍然用字符串表示. 我们都熟悉笔算的整数乘积,按照被乘数逐位与乘数求积,保存进位:当被乘数换位时,结果递增一个数量级,与先前结果求和

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

leetcode Multiply Strings

题目连接 https://leetcode.com/problems/multiply-strings/ Multiply Strings Description 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. 普通写法: class

[leetcode]Multiply Strings @ Python

原题地址:https://oj.leetcode.com/problems/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. 解题思路:两个非负数字字符串的相乘.其实就是大数乘法.算法的关键是

【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. 题解:就是让实现一个大整数乘法. 假设两个数num1和num2的长度分别是len1和len2,那么最后得到的答案,在最高位有进位的时候,就是len1+len2位,否则是len1+len2

Multiply Strings leetcode java

题目: 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. 题解: 题意就是给你两个字符串型的数字,给这两个数字做乘法. 如果直接转换成Integer做乘法就会溢出. 所以要一步一步来. 下面讲解引用自(http://leetcodeno