题目来源


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的更多相关文章

  1. [Leetcode][Python]43: Multiply Strings

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...

  2. LeetCode 043 Multiply Strings

    题目要求:Multiply Strings Given two numbers represented as strings, return multiplication of the numbers ...

  3. LeetCode 43. 字符串相乘(Multiply Strings)

    43. 字符串相乘 43. Multiply Strings 题目描述 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. ...

  4. Java for LeetCode 043 Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  5. leetcode 第42题 Multiply Strings

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

  6. 043 Multiply Strings 字符串相乘

    给定两个以字符串表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积.注意:    num1 和 num2 的长度均小于110.    num1 和 num2 均只包含数字 0 ...

  7. LeetCode(43)Multiply Strings

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

  8. leetcode面试准备:Multiply Strings

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

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

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

  10. 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) ...

随机推荐

  1. Piggy-Bank[HDU1114]

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  2. storm环境搭建

    备注——使用: 1.单机版本: 启动zkServer.nimbus.supervisor.ui服务: zkServer.sh start zkServer.sh status #查看zkserver是 ...

  3. 详细讲解css单位px,em和rem的含义以及它们之间的区别

    一.首先介绍一下px px就是css中最基本的长度单位了,用px做单位基本上没什么问题,可以做到让页面按套路精确的展现! 可但是!但可是!如果全篇用px布局会暗藏一个蛋疼的问题,就是当用户和Ctrl滚 ...

  4. XCOJ 1102 (树形DP+背包)

    题目链接: http://xcacm.hfut.edu.cn/oj/problem.php?id=1102 题目大意:树上取点.父亲出现了,其儿子包括孙子...都不能出现.给定预算,问最大值. 解题思 ...

  5. 【wikioi】2216 行星序列(线段树)

    http://wikioi.com/problem/2216/ 这题太让我感动了QAQ,让我找到了我一直以来写线段树的错误!!!! 就是,pushdown一定要放在最前面!要不然顺序会错.也就是说,当 ...

  6. Graph database_neo4j 底层存储结构分析(6)

    3.6  Node 数据存储 neo4j 中, Node 的存储是由 NodeStore 和 ArrayPropertyStore 2中类型配合来完成的. node 的label 内容是存在Array ...

  7. Powershell连接Office 365各组件的方法

    参考: http://www.exchangecn.com/office365/20150108_540.html 1. 适用于 IT 专业人员 RTW 的 Microsoft Online Serv ...

  8. 硬盘参数之TLER

    “你们根本不知道nas盘是用来干啥的,准确的说,要nas盘就是要tler技术,这样才适合用在nas上. TLER=Time-Limited Error Recovery 这么说吧,普通的硬盘(不带TL ...

  9. Laptop Issue Letter (读取Excel中指定内容,然后生成新的Excel文件)

    $xl = New-Object -ComObject "Excel.Application" $cmdbwb = $xl.Workbooks.Open("F:\Ivan ...

  10. debug阶段工作期站立会议2(进度推进)

    组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...