[leetcode]Multiply Strings @ Python
原题地址:https://oj.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.
解题思路:两个非负数字字符串的相乘。其实就是大数乘法。算法的关键是要先将两个字符串翻转过来,然后按位进行相乘,相乘后的数不要着急进位,而是存储在一个数组里面,然后将数组中的数对10进行求余(%),就是这一位的数,然后除以10,即/10,就是进位的数。注意最后要将相乘后的字符串前面的0去掉。
代码:
class Solution:
# @param num1, a string
# @param num2, a string
# @return a string
def multiply(self, num1, num2):
num1 = num1[::-1]; num2 = num2[::-1]
arr = [0 for i in range(len(num1)+len(num2))]
for i in range(len(num1)):
for j in range(len(num2)):
arr[i+j] += int(num1[i]) * int(num2[j])
ans = []
for i in range(len(arr)):
digit = arr[i] % 10
carry = arr[i] / 10
if i < len(arr)-1:
arr[i+1] += carry
ans.insert(0, str(digit))
while ans[0] == '' and len(ans) > 1:
del ans[0]
return ''.join(ans)
[leetcode]Multiply Strings @ Python的更多相关文章
- LeetCode: Multiply Strings 解题报告
Multiply StringsGiven two numbers represented as strings, return multiplication of the numbers as a ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- LeetCode:Multiply Strings
题目链接 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- LeetCode: Multiply Strings. Java
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [Leetcode] Multiply strings 字符串对应数字相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- leetcode面试准备:Multiply Strings
1 题目 Given two numbers represented as strings, return multiplication of the numbers as a string. Not ...
- [Leetcode][Python]43: Multiply Strings
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 43: Multiply Stringshttps://leetcode.co ...
- [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings
这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...
- 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) ...
随机推荐
- CodeForces700E Cool Slogans
感谢dalaoWJZ的讲解. 我们对于每一个串a[i]相当于在他parent的right集合里找一个出现位置在id-len[x]+len[parent]到id[x]-1区间的 用主席树判存在性即可. ...
- vue 直接改变数组数据不刷新
因为 JavaScript 的限制,Vue.js 不能检测到下面数组变化: 直接用索引设置元素,如 vm.items[0] = {}: 修改数据的长度,如 vm.items.length = 0. 为 ...
- centos7安装rvm
导入钥匙$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 若是提示 ...
- MEF框架简介
下面主要介绍一下MEF的架构,希望从总体上有所了解,更改OpenExpressApp后我会再写篇文章介绍一下如何在OpenExpressApp中使用MEF的. 主要示意图 各种Export提供者从目录 ...
- 一种高效的序列化方式——MessagePack
最近在弄一些数据分析方面的内容,发现很多时候数据瓶颈在模块之间的数据序列化和反序列化上了,原来项目中用的是Json,找了一圈发现Json.net在Json序列化库中已经是性能的佼佼者了,便准备从序列化 ...
- HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
小明系列故事——女友的考验 Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- LPC-LINK 2 LPC4337 TQFP144 IO
- 采用模拟账号读取Exchange server未读邮件的注意事项(链接邮箱问题)【转】
最近做项目碰到Exchange中,用EWS API方法读取的未读邮箱(ConnectingIdType.PrincipalName设置该属性的方法)附带代码部分: 核心代码 using Microso ...
- 交叉编译Python-3.6.0到aarch64/aarch32 —— 支持sqlite3
参考 https://datko.net/2013/05/10/cross-compiling-python-3-3-1-for-beaglebone-arm-angstrom/ 平台 主机: ubu ...
- How can i use iptables on centos 7?
I installed CentOS 7 with minimal configuration (os + dev tools). I am trying to open 80 port for ht ...