题目:
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
我的思路:
题目非常简单,计算两个字符串代表数字的乘积,不能使用int(),可以直接用一个字典来给出字符串不同位置代表的数值,Runtime: 28 ms(约93.57%);Memory Usage: 14.3 MB(约26.59%),复杂度O(len(num1)+len(num2))
我的解答:
class Solution:
def multiply(self, num1: str, num2: str) -> str:
def my_int(a: str):
my_dic = {str(i): i for i in range(10)}
ans = 0
for i in range(len(a)):
ans += my_dic[a[i]] * 10**(len(a) - i - 1)
return ans
return str((my_int(num1)) * (my_int(num2)))
答案:
无
该博客介绍了一种不使用内置大数库或直接转换为整数的方法来计算两个非负整数字符串的乘积。博主提供了一个Python类解决方案,通过自定义函数将字符串转换为整数,然后进行乘法运算,最后返回乘积字符串。这种方法在时间和空间效率上有一定的优势。
236

被折叠的 条评论
为什么被折叠?



