[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 stringdef 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] % 10carry = arr[i] / 10if i < len(arr)-1:arr[i+1] += carryans.insert(0, str(digit))while ans[0] == '0' and len(ans) > 1:del ans[0]return ''.join(ans)
[leetcode]Multiply Strings @ Python相关推荐
- Leetcode: Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The ...
- [LeetCode] Multiply Strings 解题报告
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 大整数乘法--leetcode Multiply Strings
大整数乘法 本文转载自http://www.cnblogs.com/TenosDoIt/p/3735309.html 我们在日常的大整数计算中,通常是把它转化为字符型计算.这道题的思路就和我们小学计算 ...
- LeetCode Multiply Strings(大整数相乘)
思路:用笔算的形式就可以了 代码如下: public class Solution {public String multiply(String num1, String num2) {StringB ...
- LeetCode算法入门- Multiply Strings -day18
LeetCode算法入门- Multiply Strings -day18 题目介绍 Given two non-negative integers num1 and num2 represented ...
- 【大数相乘】LeetCode 43. Multiply Strings
LeetCode 43. Multiply Strings Solution1:我的答案 作为一个hard题,提交一次就过真是让我hin开心啊!!! 就是方法有点笨.. class Solution ...
- LeetCode刷题实战(43):Multiply Strings
题目描述: 43 Multiply Strings 28.7% Medium Given two non-negative integers num1 and num2 represented as ...
- Leetcode#43Multiply Strings
Multiply Strings Total Accepted: 30373 Total Submissions: 145134My Submissions Question Solution Giv ...
- LeetCode刷题Python实录
使用Python的LeetCode刷题 前言 题目 1408. 数组中的字符串匹配 508. 出现次数最多的子树元素和 1089. 复写零 剑指 Offer 14- I. 剪绳子 1175. 质数排列 ...
最新文章
- 推荐一个HTML的语法高亮解析器
- 网页中的盒模型css属性,详解CSS中的Box Model盒属性的使用
- PHP IPC函数介绍---共享内存
- 初识Tcl(九):Tcl 字典
- Win10电脑开机界面设置个性签名?
- Javascript在页面加载时的执行顺序
- DeepLearnign:windows tensorflow-cmake编译
- 举例说,在命令模式(Command Pattern)
- Chromebook开发者模式Linux,Chromebook 的开发者模式和开发者版本有什么区别?
- C#中的几个实用的代码
- androidStudio使用卡顿
- COMPUTEX展 CPX论坛聚焦AI与物联网
- java post上传进度,OkHttp实现文件上传进度
- 苹果手机的测距仪怎么用
- IntellJ IDEA神器使用技巧
- 数学建模【三款超实用建模小软件!】
- DS博客作业06——图
- 论文笔记:CVPR2021 Bottom-Up Shift and Reasoning for Referring Image Segmentation
- 在Android Studio中的混淆debug与release
- adobe登录无法连接服务器未响应,adobe链接不到服务器
热门文章
- 计算机入域时域控用到的端口,AD域控制器使用端口的说明
- 用linux创建raid5,使用mdadm快速创建RAID5....(比赛后的小结!)
- mysql 存储过程怎么多行注释_数据库通过存储过程批量添加注释(模板为oracle)...
- python带我起飞 豆瓣评分_你听过后觉得爽到飞起的电音是哪一首?
- RocketMQ 实战 消息发送样例
- jquery遍历json与数组方法总结each()
- 阿里p8呕心沥血整理出来的,APP UI自动化测试思路总结,速看
- 数组第一个值_Excel公式技巧69:查找第一个非空值
- 四川师范大学大学计算机基础,四川师范大学2009-2010学年计算机基础期末考试试题.doc...
- 运维监控系统——使用API在zabbix监控系统中查看,创建及删除监控主机