[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.

解题思路:两个非负数字字符串的相乘。其实就是大数乘法。算法的关键是要先将两个字符串翻转过来,然后按位进行相乘,相乘后的数不要着急进位,而是存储在一个数组里面,然后将数组中的数对10进行求余(%),就是这一位的数,然后除以10,即/10,就是进位的数。注意最后要将相乘后的字符串前面的0去掉。

代码:


class Solution:
# @param num1, a string
# @param num2, a string
# @return a string
def multiply(self, num1, num2):
num1 = num1[::-1]; num2 = num2[::-1]
arr = [0 for i in range(len(num1)+len(num2))]
for i in range(len(num1)):
for j in range(len(num2)):
arr[i+j] += int(num1[i]) * int(num2[j])
ans = []
for i in range(len(arr)):
digit = arr[i] % 10
carry = arr[i] / 10
if i < len(arr)-1:
arr[i+1] += carry
ans.insert(0, str(digit))
while ans[0] == ‘0‘ and len(ans) > 1:
del ans[0]
return ‘‘.join(ans)

[leetcode]Multiply Strings @ Python,布布扣,bubuko.com

时间: 2024-08-25 06:09:24

[leetcode]Multiply Strings @ Python的相关文章

LeetCode: Multiply Strings [042]

[题目] 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. [题意] 给定用字符串表示的整数,返回两个数的乘积结果字符串.两个数字都非负,且能任意大. [思路] 1. 考虑其中一个数是0的情况 2. 模拟乘法运算过程 维护一个vecto

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 解题报告

Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. SOLUTION 1: 参考自http://blog.csdn.net/fightforyourdream/article/details/1737049

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. 原题链接:https://oj.leetcode.com/problems/multiply-strings/ 按照乘法原理,从个位开始一位一位相乘相加. public class Mu

[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的数值范围的约束,那么我们该如何来计算

LeetCode: Multiply Strings. 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. public class Solution { //模拟手算乘法 public String multiply(String num1, String num2) { int n = n

【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

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