【LeetCode题意分析&解答】43. 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.
题意分析:
本题是求两个用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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- 【LeetCode题意分析&解答】38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- 【LeetCode题意分析&解答】41. First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- 【LeetCode题意分析&解答】39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...
- 【LeetCode题意分析&解答】36. Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 【LeetCode题意分析&解答】34. Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value. You ...
随机推荐
- [Script]EBS里查看模块的版本、文件的版本信息【Z】
系统版本信息 装了哪些模块,以及版本信息 select 'Current Application Release: '||ver||' ('||bug||')' "Description&q ...
- Quartz 2D 概述
Quartz 2D是一个二维图形绘制引擎,支持iOS环境和Mac OS X环境.我们可以使用Quartz 2D API来实现许多功能,如基本路径的绘制.透明度.描影.绘制阴影.透明层.颜色管理.反锯齿 ...
- ecshop二次开发之购物车常见问题
1.ecshop二次开发中保存注册用户购物车数据解决方法:ecshop购物车是数据库中cart表来支持的,在ecshop表中rec_id是编号,user_id是注册用户的id,session_id表示 ...
- Tomcat 设置为服务使用脚本 service
进入到Tomcat的bin目录下,如果使用的是Windows系统则使用service.bat进行操作;Linux系统则使用service.sh进行. service.bat install/remov ...
- Docker容器的跨主机连接
Docker容器的跨主机连接 Docker容器的跨主机连接 使用网桥实现跨主机容器连接 网络拓扑 网络连接使用的是Bridge 操作 修改/etc/network/interfaces文件,分配静态I ...
- codeforces 8D Two Friends 二分+ 判断三个圆是否有公共交点
题目链接 有两个人x, y, 现在在A点, x要直接去B点, y要先去C点在去B点, 现在给出x, y两人可以行走的最大距离T1, T2, 求出他们从A点出发之后, 可以走的最长的公共路径. 我们先看 ...
- 全角和半角相互转换(C语言实现)
目前,我们接触的汉字编码主要包括GBK和GB2312.其中,GB2312又称国标码,它是一个简化字的编码规范,也包括其他的符号.字母.日文假名等,共7445个图形字符,其中汉字占6763个.我们平时说 ...
- Allegro的优点与缺点
记得刚毕业出来时就在某台商工作,用的就是allegro,从此上了贼船就下不来了--.其实还用过pcad,protel,powerpcb(以下简称3p,加上pads就4p了,呵呵--).至于mentor ...
- 可以把一堆dll文件放到程序目录下的一个自建目录里面
窦宁波大哥哥的那篇文章的这种写法还是很有参考价值的. QString strLibPath(QDir::toNativeSeparators(QApplication::applicationDirP ...
- poj 3323 Matrix Power Series (矩阵乘法 非递归形式)
为了搞自动机+矩阵的题目,特来学习矩阵快速幂..........非递归形式的求Sum(A+A^2+...+A^k)不是很懂,继续弄懂................不过代码简洁明了很多,亮神很给力 # ...