题目来源


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. 武林[HDU1107]

    武林 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  2. QPushButton 与 QListWidget 的按键响应

    在Qt中添加按钮或表格控件时需要添加其按键响应,一般来说有两种方法,一种是直接实现其响应函数,第二种是自己写一个响应函数,然后用Qt的信号槽将它们连接起来.愚以为第一种方法更为简单一些. 声明这些控件 ...

  3. hdu Can you solve this equation?

    本题是一道二分的题,核心就是mi的大小,即精度的取值.因为原函数是一个单调递增的函数,所以可以确定y的取值范围,并且在范围内的每一个y的值,一定至少存在一个x与其对应.刚开始我将取二分这个环节用一个函 ...

  4. java web工程之Hibernate

    java web添加structs特性后再添加Hibernate特性,这可以通过右键工程->my eclipse出现工具条选中相应的条目,添加相应的属性, 添加完Hibernate后建立与数据库 ...

  5. [转]动态调用webservice时 ServiceDescriptionImporter类在vs2010无法引用的解决方法

    本文转自:http://blog.csdn.net/limlimlim/article/details/8647038 [导读]ServiceDescriptionImporter是创建Web Ser ...

  6. 根据BIOS信息修改主机名

    Dell: Rename-Computer -NewName ("CNHZPD-" + (Get-WmiObject -class win32_Bios).SerialNumber ...

  7. 【液晶模块系列基础视频】5.1X-GUI字体驱动1

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  8. HDU 1284 思维上的水题

    其实如果想出了方法真的好水的说... 然而一开始想了好久都没想出来... 最后看了一下最大数据才32768 可以直接枚举...枚举每个硬币的数量 看看后来能不能凑够n 因为还是怕超时..(虽然只有3乘 ...

  9. 《Java核心技术卷一》笔记 多线程同步(底层实现)

    一.锁的基本原理 多个线程同时对共享的同一数据存取 ,在这种竞争条件下如果不进行同步很可能会造成数据的讹误. 例如:有一个共享变量int sum=0, 一个线程正调用 sum+=10,另一个线程正好也 ...

  10. django 1.8 日志配置

    django 1.8 日志配置 以下为setings配置logging代码片段 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(_ ...