[Leetcode][Python]43: Multiply Strings
# -*- 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的更多相关文章
- 【LeetCode】43. Multiply Strings
Multiply Strings Given two numbers represented as strings, return multiplication of the numbers as a ...
- 【LeetCode】43. Multiply Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【一天一道LeetCode】#43. Multiply Strings
一天一道LeetCode系列 (一)题目 Given two numbers represented as strings, return multiplication of the numbers ...
- LeetCode:43. Multiply Strings (Medium)
1. 原题链接 https://leetcode.com/problems/multiply-strings/description/ 2. 题目要求 给定两个String类型的正整数num1.num ...
- 【leetcode】43. Multiply Strings(大数相乘)
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- [LeetCode] 43. Multiply Strings ☆☆☆(字符串相乘)
转载:43. Multiply Strings 题目描述 就是两个数相乘,输出结果,只不过数字很大很大,都是用 String 存储的.也就是传说中的大数相乘. 解法一 我们就模仿我们在纸上做乘法的过程 ...
- [LeetCode] 43. Multiply Strings 字符串相乘
Given two non-negative integers num1 and num2represented as strings, return the product of num1 and ...
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
随机推荐
- IOS深入学习(4)之Coordinate System
1 前言 在IOS中相信大家会经常跟一些bounds,frame之类的打交道,这不免会涉及坐标系统,今天我们就来介绍一下Coordinate System(坐标系). 2 详述 坐标系统是定位,大小, ...
- app行业发展趋势
近日,移动开放平台发布了2014年第一季度App开发行业报告.报告中对目前国内app开发者的分布情况,个人开发者和企业开发者的开发领域,相应比例以及提交应用过程中出现的问题做出统计,为如何建立一个更好 ...
- CSS发光边框文本框效果
7,166 次阅读 ‹ NSH Blog 网页设计 CSS发光边框文本框效果 或许你看过Safari浏览器下,任何输入框都会有一个发光的蓝色边框,这不单纯只是蓝色边框而已,其实包含了许多CSS3技巧知 ...
- nginx+vaadin配置
nginx+Vaadin的特殊性在于配置WEBSOCKET或LONG_POLLING.网上资料不多,自己多次尝试配置都不成功,后来终于找到这篇说明才得以配置成功,使用效果不错,介绍如下. 1./etc ...
- UESTC_How many good substrings CDOJ 1026
Icerain likes strings very much. Especially the strings only consist of 0 and 1,she call them easy s ...
- Binary Search Tree Iterator 解答
Question Implement an iterator over a binary search tree (BST). Your iterator will be initialized wi ...
- java没有条件编译
摘自http://maosidiaoxian.iteye.com/blog/1290740 条件编译绝对是一个好东西.如在C或CPP中,可以通过预处理语句来实现条件编译.代码如下: #IFDEF DE ...
- Hide a file in a picture
有时候.假设你想在电脑上隐藏关键的文件而不想让其它人看见.你会怎么做呢?找一个专业的工具?为目录设置password?更改文件属性?这些方法可行.但它们可能不太方便和安全.这里,我给大家共享一个在图片 ...
- GCD 和延时调用
因为 Playground 不进行特别配置的话是无法在线程中进行调度的,因此本节中的示例代码需要在 Xcode 项目环境中运行.在 Playground 中可能无法得到正确的结果. GCD 是一种非常 ...
- Linux常见目录作用
Linux中一切皆文件 文件类型: 一般文件 - 目录文件 d 链接文件 l 块设备 b (以块为单位进行操作,比如硬盘) 字符设备 c (以字符为单位进行操作,比如主存) socket ...