Leetcode: Multiply Strings
来源:互联网 发布:今年网络最流行歌曲 编辑:程序博客网 时间:2024/05/16 11:09
Question
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.
Hide Tags Math String
Hide Similar Problems (M) Add Two Numbers (E) Plus One (E) Add Binary
Solution
Get idea from here1, here2
class Solution(object): def multiply(self, num1, num2): """ :type num1: str :type num2: str :rtype: str """ if len(num1)==0 or len(num2)==0 or num1=='0' or num2=='0': return '0' num1, num2 = list(num1), list(num2) num1.reverse() num2.reverse() res = [0]*(len(num1)+len(num2)) for ind1 in range(len(num1)): a = ord(num1[ind1]) - ord('0') for ind2 in range(len(num2)): b = ord(num2[ind2]) - ord('0') res[ind1+ind2] += a*b ress = [] carry = 0 for ind in range(len(res)): digit = res[ind]%10 ress = [(digit+carry)%10] + ress carry = res[ind]/10 + (digit+carry)/10 if len(ress)>0 and ress[0]==0: ress = ress[1:] ress = [str(elem) for elem in ress] ress = ''.join(ress) return ress
0 0
- LeetCode: Multiply Strings
- LeetCode Multiply Strings
- LeetCode: Multiply Strings
- [Leetcode] Multiply Strings
- [LeetCode] Multiply Strings
- [Leetcode] Multiply Strings
- LeetCode 45: Multiply Strings
- [LeetCode]Multiply Strings
- [LeetCode] Multiply Strings
- LeetCode-Multiply Strings
- [leetcode] Multiply Strings
- LeetCode - Multiply Strings
- Leetcode: Multiply Strings
- LeetCode题解:Multiply Strings
- leetcode Multiply Strings
- LeetCode Multiply Strings
- LeetCode | Multiply Strings
- [LeetCode] Multiply Strings
- CentOS 安装python MySQLdb模块
- 制度是绝情的,管理是无情的,执行是合情的
- 批量生成渠道包
- Mysql分库分表方案
- asp.net 中 .ASPX 与.CS文件的关系
- Leetcode: Multiply Strings
- mysql常用指令
- 如何实现Linux多台服务器间的文件双向同步(二)
- IOS面试题
- Win10开发究竟能实现哪些牛逼的功能
- yarn基本架构
- 约瑟夫问题
- epoll系列系统调用
- cc/gcc 不同平台的差异