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. EF实体框架-从数据库更新模型 一部分表的外键(导航属性)无法显示

    从数据库更新模型 要想让数据库表之间的外键关系 显示到实体模型的导航属性中去. 表的外键 对应另一张表的字段要是主键,唯一键显示不出来

  2. 自定义构造方法和description方法

    知识回顾在第5讲中已经介绍了如何定义类和创建并初始化对象,比如有Student这个类1.Student.h 1 #import <Foundation/Foundation.h>23@in ...

  3. hdu4745

    区间DP,这类题目还是非常常见的,可惜平时都不怎么在意.一到比赛就弱得像鸟一样,真心囧. 题目要求很简单,就是一个最长的回文子序列,输出该子序列的长度. 区间DP,最常用的一种策略(类似于数学归纳法) ...

  4. HTTP填坑

    HTTP知识填坑 .note-content {font-family: "Helvetica Neue",Arial,"Hiragino Sans GB",& ...

  5. ueditor插件 -- 插入填空题

    插入填空题,一个看似小小的需求,但是却是折腾了很9.主要产品那边的要求,空格上面要标有序号,并且再页面当中能够同步空格答案列表. 1.ueditor插件 插件入门,官方的例子还是很简单直接的,对于我们 ...

  6. B/S结构和C/S结构

    概念: C/S结构,即Client/Server(客户机/服务器)结构,是大家熟知的软件系统体系结构,通过将任务合理分配到Client端和Server端,降低了系统的通讯开销,可以充分利用两端硬件环境 ...

  7. php curl函数实例

    <?php function login(){ $url = 'http://jspatch.qq.com/offline/check?qver=6.2.0.427&hver=0& ...

  8. Nginx+PostgreSQL+Django+UWSGI搭建

    最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx.常见的 django webapp 部署方式采用FCGI 或 WSGI ...

  9. PICT安装及使用

    一:PICT安装 1.下载pict33.msi:http://vdisk.weibo.com/s/d6k2tcgXDa7Eq 2.安装: 二:PICT的使用 1.在F:\PICT 目录下,新建一个tx ...

  10. Eclipse下如何打开ftl文件

    ftl文件是freemarker模板文件,用freemarker时,常用该文件模板:但是该文件在eclipse编辑时,黑白底的,没有任何提示,下面介绍如何用JSP编辑器打开该文件. 工具/原料   e ...