题目来源


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. BZOJ3290 : Theresa与数据结构

    CANCEL操作可以看作删点,X坐标可以离散化 将询问按Z坐标差分,转化成两个求Z<=某个数的和的询问 将操作CDQ分治 每次将前一半的修改.后一半的查询按照Z坐标排序 然后扫描线,每到一个询问 ...

  2. 发送JS错误日志到服务器

    JS记录错误日志/捕捉错误 //onerror提供异常信息,文件路径和发生错误代码的行数的三个参数. window.onerror = function(e,url,index){ var msg = ...

  3. js中等性操作符(==)、关系操作符(<,>)和布尔操作符(!)比较规则

    最近一直在笔试面试,经常碰到例如 123=='123'.'abc'==true等问题,其中有答对的,也有答错的,主要原因还是对ECMAScript的规范没有理解清楚,很多题目没有具体分析所导致.现查阅 ...

  4. POJ 2104 K-th Number(划分树)

    题目链接 参考HH大神的模版.对其中一些转移,还没想清楚,大体明白上是怎么回事了,划分树就是类似快排,但有点点区别的.多做几个题,慢慢理解. #include <cstdio> #incl ...

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

    3.5 Property 的存储 下面是neo4j graph db 中,Property数据存储对应的文件: neostore.propertystore.db neostore.propertys ...

  6. iOS - AVAudioPlayer 音频播放

    前言 NS_CLASS_AVAILABLE(10_7, 2_2) @interface AVAudioPlayer : NSObject @available(iOS 2.2, *) public c ...

  7. 【iHMI43 4.3寸液晶模块】demo例程(库函数版)发布

    一.说 明: 1.iHMI43 演示程序(0.15版)的库函数版:此程序仅用于 iHMI43 液晶模块: 2.各外设驱动均使用库函数模式操作,其他程序可参考本例子修改: 3.如果下载不进去,请重新配置 ...

  8. GDUT——1169: Krito的讨伐(优先队列BFS)

    1169: Krito的讨伐 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 619  Solved: 102 Description Krito终于干 ...

  9. 2016.09.14,英语,《Using English at Work》全书笔记

    半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...

  10. Apache Storm技术实战之2 -- BasicDRPCTopology

    欢迎转载,转载请注明出处,徽沪一郎. 本文通过BasicDRPCTopology的实例来分析DRPCTopology在提交的时候, Topology中究竟含有哪些内容? BasicDRPCTopolo ...