码迷,mamicode.com
首页 > 编程语言 > 详细

[leetcode]Multiply Strings @ Python

时间:2014-06-12 21:43:03      阅读:344      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

原题地址: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去掉。

代码:

bubuko.com,布布扣
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)
bubuko.com,布布扣

 

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

[leetcode]Multiply Strings @ Python

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/zuoyuan/p/3781515.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!