# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 43: Multiply Strings
https://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. === Comments by Dabay===
用小学学的乘法公式。
''' class Solution:
# @param num1, a string
# @param num2, a string
# @return a string
def multiply(self, num1, num2):
if num1 == "0" or num2 == "0":
return "0"
# if len(num1) < len(num2):
# num1, num2 = num2, num1
res = ""
for j in reversed(xrange(len(num2))):
tmp = ""
carry = 0
for i in reversed(xrange(len(num1))):
x = int(num1[i]) * int(num2[j]) + carry
carry = x / 10
x = x % 10
tmp = str(x) + tmp
if carry != 0:
tmp = str(carry) + tmp
res = self.num_add(res, tmp + "0" * (len(num2)-1-j))
return res def num_add(self, num1, num2):
if len(num1) > len(num2):
num2 = num2.zfill(len(num1))
else:
num1 = num1.zfill(len(num2))
res = ""
carry = 0
for i in reversed(xrange(len(num1))):
x = int(num1[i]) + int(num2[i]) + carry
if x >= 10:
x -= 10
carry = 1
else:
carry = 0
res = str(x) + res
if carry == 1:
res = "1" + res
return res def main():
sol = Solution()
num1 = "999"
num2 = "999"
print sol.num_add(num1, num2)
print sol.multiply(num1, num2) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]43: Multiply Strings的更多相关文章

  1. 【LeetCode】43. Multiply Strings

    Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...

  2. 【LeetCode】43. Multiply Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【一天一道LeetCode】#43. Multiply Strings

    一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...

  4. LeetCode:43. Multiply Strings (Medium)

    1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...

  5. 【leetcode】43. Multiply Strings(大数相乘)

    Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...

  6. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  7. [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)

    转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...

  8. [LeetCode] 43. Multiply Strings 字符串相乘

    Given two non-negative integers num1 and num2represented as strings, return the product of num1 and  ...

  9. leetcode面试准备:Multiply Strings

    1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...

随机推荐

  1. qt 多线程之间通讯

    问题描述:界面线程MainApp为主线程,工作线程MyThread为一子线程,从工作线程向主线程传递字符串用于在主线程中显示. Qt的信号与槽机制可以将任何继承自QObject类的对象捆绑在一起,使不 ...

  2. HttpWebResponse类

    HttpWebResponse类的作用用于在客户端获取返回的响应的信息,还记得HttpResponse类吗?你是否在写B/S程序的时候,经常用到Response.Write()呢? HttpRespo ...

  3. tar解压错误

    # tar -zxvf aaa.tar.gz    tar: This does not look like a tar archive   tar: Skipping to next header ...

  4. 用来控制 XML 序列化的属性

    通过将下表中的属性应用于类和类成员,可以控制 XmlSerializer 序列化或反序列化该类的实例的方式.若要了解这些属性如何控制 XML 序列化,请参见使用属性控制 XML 序列化. 这些属性还可 ...

  5. 走进C++程序世界------继承和派生

    继承和派生 继承是面向对象编程语言的最重要方面之一,正确的使用继承可编写出设计良好,容易于维护和扩展的应用程序.下面是在其他博客中的总结: ****************************** ...

  6. iOS开发-21UINavigationController导航控制器初始化 导航控制器栈的push和pop跳转理解

    (1)导航控制器初始化的时候一般都有一个根视图控制器,导航控制器相当于一个栈,里面装的是视图控制器,最先进去的在最下面,最后进去的在最上面.在最上面的那个视图控制器的视图就是这个导航控制器对外展示的界 ...

  7. NET基础课--JIT编译器如何工作1

    1..Net运行时调用JIT编译器,用来把由C#编译器生成的IL指令编译成机器代码.这一任务在应用程序的运行期间是分步进行的.JIT并不是在程序一开始就编译整个应用程序,取而代之的是,CLR是一个函数 ...

  8. NET基础课--异常处理X

    通常不建议如下的捕获方式  正确的方法是:某一功能函数的入口捕获基本异常即exception,分支方法或片段方法中捕获特定异常 高级: 另附:Fxcop异常监控工具

  9. SFTP CONFIGURATION IN FLASHFXP PROGRAM

    This is a brief guide on how to configure the FlashFXP FTP client to log on to the domain web space ...

  10. Github android客户端源代码分析之一:环境搭建

    1.下载相应的包及项目,参考https://github.com/github/android/wiki/Building-From-Eclipse. 2.若需查看某些包的源文件或者javadoc,则 ...