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.

题意分析:

  本题是求两个用string表示的非负大数的乘积,乘数可以是任意大小。

解答:

  可以用一个临时List表示乘积的每一位,然后对两个乘数每一位两两相乘,并将结果填到相应的List坐标中即可。

AC代码:

class Solution(object):
def multiply(self, num1, num2):
ret_list = [0] * (len(num1) + len(num2))
for i, vi in enumerate(reversed(num1)):
for j, vj in enumerate(reversed(num2)):
ret_list[i + j] += int(vi) * int(vj)
ret_list[i + j + 1] += ret_list[i + j] / 10
ret_list[i + j] %= 10
while len(ret_list) > 1 and ret_list[-1] == 0:
ret_list.pop()
return ''.join(map(str, ret_list[::-1]))

【LeetCode题意分析&解答】43. Multiply Strings的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. 【LeetCode题意分析&解答】38. Count and Say

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  5. 【LeetCode题意分析&解答】42. Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  6. 【LeetCode题意分析&解答】41. First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  7. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  8. 【LeetCode题意分析&解答】36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 【LeetCode题意分析&解答】34. Search for a Range

    Given a sorted array of integers, find the starting and ending position of a given target value. You ...

随机推荐

  1. i = i++ 在java字节码层面的分析

    有这么一段代码: package zl.test; public class PcodeTest { /** * @param args */ public static void main(Stri ...

  2. Constructor JavaScript构造器模式。

    构造器模式 : Constructor模式中, 通过在构造器前面加 new 关键字, 告诉JavaScript 像使用构造器一样实例化一个新对象,并且对象成员由该函数定义. 构造器内, 使用this ...

  3. hadoop笔记之Hive的管理(web界面方式)

    Hive的管理(二) Hive的管理(二) Web界面方式 端口号9999 启动方式:hive --service hwi 通过浏览器来访问:http://<IP地址>:9999/hwi/ ...

  4. 说出x的结果,并解释为什么?

    var x = 1; if(function f(){}){ x += typeof f; } x; //x的结果是? x=1undefined 首先是 if表达式的问题 if括号里,不一定非要用== ...

  5. CeontOS7安装ansible

    安装方法一. 第一步:安装epel rpm -ivh http://mirror.pnl.gov/epel/7/x86_64/e/epel-release-7-5.noarch.rpm 第二步:安装a ...

  6. 2.java.lang.NullPointerException(空指针异常)

    调用了未经初始化的对象或者是不存在的对象 经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等.对数组操作中出现空指针, 即把数组的初始化和数组元素的初始化混淆起来 ...

  7. ArcEngine做栅格数据拉伸

    //获得已打开的栅格数据 IRasterLayer rasterLayer = new RasterLayerClass(); rasterLayer = (IRasterLayer)axMapCon ...

  8. 使用百度语音识别REST API,做全平台语音识别

    百度语音开发介绍文档: http://yuyin.baidu.com/docs/asr# 使用语音识别,需要在百度申请一个应用,然后拿到API Key和Secret Key,然后才可以使用语音识别 p ...

  9. openstack 开发step-by-step

    Set up your Open Stack There are several ways to deploy openstack, Devstack is easily for developer ...

  10. 应用程序正常初始化(0xc015002)失败解决方法

    VS2005 sidebyside manifest error Microsoft.VC80.MFC Microsoft.VC80.CRT Microsoft.VC80.MFCLOC msvcr80 ...