[LeetCode]题解(python):043-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.
题意分析
Input: two numbers expressed as string
Output:the multiply of the two sums
Conditions:数可以无限大,做两个数的乘法
如:"23650379502752" 和 "865382861454"
结果:"20466633088564555427721408"
题目思路
首先将两个str转化为整数的list,然后考虑到乘积的位数必然小于等于len(str1)+len(str2),先初始化乘积为0的list,然后按照位数相乘的规律去做
注意:
1 最后结果需要将大数的0去掉,同时如果结果为0需要返回串“0”
2 翻转:mul.reverse()
AC代码(Python)
_author_ = "YE"
# -*- coding:utf-8 -*- class Solution(object):
def multiply(self, num1, num2):
"""
:type num1: str
:type num2: str
:rtype: str
"""
len1 = len(num1)
len2 = len(num2) list1 = [0 for i in range(len1)]
list2 = [0 for i in range(len2)] for i in range(len1):
list1[len1 - 1 - i] = int(num1[i])
for i in range(len2):
list2[len2 - 1 -i] = int(num2[i]) # print(list1,list2) mul = [0 for i in range(len1 + len2)] for i in range(len2):
carry = 0
for j in range(len1):
mul[i + j] = mul[i + j] + carry + (list2[i] * list1[j]) % 10 carry = (list2[i] * list1[j]) // 10 if mul[i + j] >= 10:
carry = carry + mul[i + j] // 10
mul[i + j] = mul[i + j] % 10 if carry > 0:
mul[i + len1] += carry
if mul[i + len1] > 10:
mul[i + len1] = mul[i + len1] % 10
carry += mul[i + len1] // 10 index = len1 + len2 - 1
while index >= 0:
if mul[index] > 0:
break
index -= 1 if index + 1 < len1 + len2:
mul[index+1:] = [] mul.reverse() s = ''
for i in range(len(mul)):
s += str(mul[i])
if s == '':
s = ''
return s str1 = ''
str2 = ''
s = Solution() print(s.multiply(str1,str2))
[LeetCode]题解(python):043-Multiply Strings的更多相关文章
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- LeetCode 043 Multiply Strings
题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...
- LeetCode 43. 字符串相乘(Multiply Strings)
43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...
- Java for LeetCode 043 Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode 第42题 Multiply Strings
题目:Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- 043 Multiply Strings 字符串相乘
给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意: num1 和 num2 的长度均小于110. num1 和 num2 均只包含数字 0 ...
- LeetCode(43)Multiply Strings
题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Note: ...
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- LeetCode解题报告—— Combination Sum & Combination Sum II & Multiply Strings
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T) ...
随机推荐
- Open the Lock[HDU1195]
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- python模块之codecs
http://blog.csdn.net/suofiya2008/article/details/5579413
- linux 好用的程序
来自 http://www.cnblogs.com/skyseraph/archive/2010/10/30/1865280.html * Shell: bash.我使用 bash 的 vi 命令 ...
- QInputDialog 使用方法
在Qt中,如果想快速生成一个对话框,可以和用户进行简单的交互,而不需要写一个新的类的时候,就要用到QInputDialog类,这个类就是专门用来建立简单对话框的,其主要能建下列几种对话框:
- [转]支付宝接口程序、文档及解读(ASP.NET)
本文转自:http://www.cnblogs.com/blodfox777/archive/2009/11/03/1595223.html 最近需要为网站加入支付宝的充值接口,而目前关于支付宝接口开 ...
- [转]Jquery通用开源框架之【ejq.js】
ejq是一款非常小巧的JS工具库,未压缩才50K,在jquery的基础上对jquery缺失部分作了很好的弥补作用. 优点: 1.具有内置的模板解析引擎语法和angularjs相近减少学习成本 2.能够 ...
- 内存调试工具Electric Fence
源码下载地址 注:官方地址下载不了,可能不再维护了,此是一个老项目 efence中相关环境变量控制: 302 /* 303 * See if the user wants to allow mallo ...
- php isset() empty() 区别, 判断 变量存在与否神器
先看PHP手册: bool empty ( mixed $var ) 判断一个变量是否被认为是空的.当一个变量并不存在,或者它的值等同于FALSE,那么它会被认为不存在.如果变量不存在的话,empty ...
- UnrealEngine4 PBR Shading Model 概述
虽然是概述,但内容并还是有些多,写上一篇PBR概念概述后,也在考虑怎么继续下去,最后还是觉得先多写一些东西再慢慢总结,所以还是尽量把这些年PBR相关的Paper精粹沉淀下来吧. 因为UE4 ...
- 基础C++ functional
函数对象:可以定义函数对象. sort(this->sprites.begin(), this->sprites.end(), [](const struct SpriteInfo &am ...