题目来源


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. Unix Shell中单引号、双引号字符、反斜杠、反引号的使用[转]

    在执行shell脚本的时候,shell将会对脚本中的行进行解释,然后执行:对于一些特殊处理的句子,我们可以使用引号或者反斜线来避免shell解释执行之.如下,当在命令行中输入:echo *child. ...

  2. 如何获取checkboxlist的多个选中项

    string[] array = dt.Rows[0]["s_type"].ToString().Split('|');                foreach (ListI ...

  3. 利用Oracle的row_number() over函数消除重复的记录

    .select d.id,d.outer_code from dict_depts_source d order by outer_code(查看重复数据) .select d.id,d.outer_ ...

  4. iOS移动开发周报-第23期

    iOS移动开发周报-第23期 [摘要]:本期iOS移动开发周报带来如下内容:苹果发出9月9日发布会邀请函,AFNetworking2.0源码解析,objc与鸭子对象,Protecting iOS Ap ...

  5. Resources

    McGuire Computer Graphics Data http://mesh.brown.edu/calibration/software.html Pixar Online Library ...

  6. 使用CSS3实现百叶窗

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  7. jquery修改a标签的href链接和文字

    可以先体验一下效果:http://keleyi.com/keleyi/phtml/jquery/2.htm 以下修改a标签的href链接和修改文字的代码: <script type=" ...

  8. RESTful和JAX-RS

    一.简介 Java Web有很多成熟的框架,主要可以分为两类Web Application和Web Services.用于Web Application的框架包括官方的Servlet/JSP, JST ...

  9. Frenetic Python实验(二)

    实验3 packet_in_out 目的:模拟一个普通的双端口中继器. This application implements a very simple 2 port repeater where ...

  10. nor flash和nand flash的区别

    NOR和NAND是现在市场上两种主要的非易失闪存技术.Intel于1988年首先开发出NOR flash技术,彻底改变了原先由EPROM和EEPROM一统天下的局面.紧接着,1989年,东芝公司发表了 ...