43. Multiply Strings leetcode python 2016 new season
2016-01-25 04:46
555 查看
Total Accepted: 51879 Total
Submissions: 231091 Difficulty: Medium
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.
Show Company Tags
Show Tags
Show Similar Problems
Have you met this question in a real interview?
Yes
No
Discuss
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
num1_len = len(num1)
num2_len = len(num2)
arr = [0 for _ in range(num1_len + num2_len)]
num1 = num1[::-1]
num2 = num2[::-1]
result = []
for i in range(num1_len):
for j in range(num2_len):
arr[i + j] += int(num1[i]) * int(num2[j])
for arr_i in range(num1_len + num2_len):
digit = arr[arr_i] % 10
carry = arr[arr_i] / 10
if arr_i < num1_len + num2_len - 1:
arr[arr_i + 1] += carry
result.append(str(digit))
result.reverse()
while result[0] == '0' and len(result) > 1:
del result[0]
return "".join(result)
Submissions: 231091 Difficulty: Medium
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.
Show Company Tags
Show Tags
Show Similar Problems
Have you met this question in a real interview?
Yes
No
Discuss
class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
num1_len = len(num1)
num2_len = len(num2)
arr = [0 for _ in range(num1_len + num2_len)]
num1 = num1[::-1]
num2 = num2[::-1]
result = []
for i in range(num1_len):
for j in range(num2_len):
arr[i + j] += int(num1[i]) * int(num2[j])
for arr_i in range(num1_len + num2_len):
digit = arr[arr_i] % 10
carry = arr[arr_i] / 10
if arr_i < num1_len + num2_len - 1:
arr[arr_i + 1] += carry
result.append(str(digit))
result.reverse()
while result[0] == '0' and len(result) > 1:
del result[0]
return "".join(result)
相关文章推荐
- Python常用函数
- Python如何进行cross validation training
- Python/Keras如何将给定的数据集打乱
- Python-装饰器以及对带有参数的装饰器的理解
- Python 删除列表中的重复数据
- 21行Python代码实现拼写检查器
- 基于Python Shell获取hostname和fqdn释疑
- 谈谈Python进行验证码识别的一些想法
- Python网站验证码识别
- 详解Python验证码识别
- Python开发如何在ubuntu 15.10 上配置vim
- Python编程中对文件和存储器的读写示例
- python黑魔法之编码转换
- Python IDE
- Python之路——线程池
- [Leetcode]126. Word Ladder II python
- Python 各种测试框架简介(二):unittest
- elipse+pydev pydeliciou导入问题
- python read()
- 在eclipes 中配置python