[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) ...
随机推荐
- centos7 静默安装oracle
系统centos7.4 mini 关闭selinux.firewalld 配置主机名: hostnamectl set-hostname --static oracle 之前说oracle不认cen ...
- DEX文件类型和虚拟机(摘抄)
DEX文件类型是Android平台上可执行文件的类型. Dalvik是Google公司自己设计用于Android平台的Java虚拟机.Dalvik虚拟机是Google等厂商合作开发的Android移动 ...
- JAVA初学练手项目,学生管理系统
github地址:https://github.com/qscqesze/StudentManager 简单描述一下: UI层面用于接受用户的处理信息,然后移交给StudentDao去处理数据. 其中 ...
- Codeforces Round #371 (Div. 2) B. Filya and Homework 水题
B. Filya and Homework 题目连接: http://codeforces.com/contest/714/problem/B Description Today, hedgehog ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM
今天想配置PI OPC SERVER的DCOM设置,但是发现在“运行dcomcnfg->组件服务-计算机-我的电脑-DCOM设置”中找不到PI OSI DA Server.如下图所示 这是以前从 ...
- oracle 删除字段中空格
update sales_report set region = REGEXP_REPLACE(region, '( ){1,}', '')
- [golang 易犯错误] golang 局部变量初始化:=的陷阱
我们知道,golang中局部变量初始化方法(使用“:=”创建并赋值),让我们在使用变量时很方便.但是,这也是易犯错误的地方之一.特别是这个初始化符还支持多个变量同时初始化,更特别的是它还支持原有变量赋 ...
- 为什要使用预编译SQL?(转)
本文转自https://www.cnblogs.com/zouqin/p/5314827.html 今天在研发部技术大牛的指点下,我终于明白了为什么要使用SQL预编译的形式执行数据库JDBC:
- Android Service总结03 之被启动的服务 -- Started Service
Android Service总结03 之被启动的服务 -- Started Service 版本 版本说明 发布时间 发布人 V1.0 添加了Service的介绍和示例 2013-03-17 Sky ...