标签:
class Solution: # @param {string} num1 # @param {string} num2 # @return {string} def multiply(self, num1, num2): if num1 == "0" or num2 == "0": return "0" result = [0] * (len(num1) + len(num2)) num1 = [int(i) for i in num1] num2 = [int(i) for i in num2] for index1 in xrange(len(num1)): multiplier = num1[index1] temp = [i*multiplier for i in num2] # Multiply temp.extend([0] * (len(num1) - index1 - 1)) # Shift for resIndex in xrange(1, len(temp) + 1): result[-resIndex] += temp[-resIndex] for resIndex in xrange(len(result)-1, 0, -1): result[resIndex-1] += result[resIndex] // 10 result[resIndex] %= 10 result = "".join([str(i) for i in result]).lstrip("0") return result
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)
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/andrew9tech/article/details/46711081