当前位置:网站首页>[force buckle]43 String multiplication
[force buckle]43 String multiplication
2022-07-06 05:28:00 【wingaso】
The original way
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == '0' or num2 == '0':return '0'
sgn = (num1[0] == '-') ^ (num2[0] == '-')
num1 = list(num1[::-1])
num2 = list(num2[::-1])
if num1[-1] == '-':num1.pop()
if num2[-1] == '-':num2.pop()
num1 = list(map(int,num1))
num2 = list(map(int,num2))
res = [0] * 1000
t = 0
for i in range(len(num1)):
for j in range(len(num2)):
res[i + j] += num1[i] * num2[j] + t
t = res[i + j] // 10
res[i + j] %= 10
res[i + len(num2)] += t
t = 0
res = list(map(str,res))
while res[-1] == '0': res.pop()
if sgn:res.append('-')
return ''.join(res[::-1])
Improved practice
class Solution:
def multiply(self, num1: str, num2: str) -> str:
if num1 == '0' or num2 == '0':return '0'
sgn = (num1[0] == '-') ^ (num2[0] == '-')
num1 = list(num1[::-1])
num2 = list(num2[::-1])
if num1[-1] == '-':num1.pop()
if num2[-1] == '-':num2.pop()
num1 = list(map(int,num1))
num2 = list(map(int,num2))
res = [0] * 1000
for i in range(len(num1)):
for j in range(len(num2)):
res[i + j] += num1[i] * num2[j]
for i in range(len(num1) + len(num2)):
res[i + 1] += res[i] // 10
res[i] %= 10
res = list(map(str,res))
while res[-1] == '0': res.pop()
if sgn:res.append('-')
return ''.join(res[::-1])
The original method is a simple simulation of integer multiplication in primary school , And the code of the improved version , Separate the process of bit by bit multiplication from the process of carry , Although there is little difference in code length , But the difficulty of writing the code is slightly reduced .
As shown in the figure .
Topic link
Originality is not easy. , Thank you for your support !
边栏推荐
- Fiddler installed the certificate, or prompted that the certificate is invalid
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 03. Login of development blog project
- [detailed explanation of Huawei machine test] statistics of shooting competition results
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- 05. Security of blog project
- 04. 项目博客之日志
- Pointer classic written test questions
- Sorting out the knowledge points of multicast and broadcasting
- After the project is released, index Html is cached
猜你喜欢
【云原生】3.1 Kubernetes平台安装KubeSpher
Principle and performance analysis of lepton lossless compression
Ora-01779: the column corresponding to the non key value saving table cannot be modified
GAMES202-WebGL中shader的編譯和連接(了解向)
Vulhub vulnerability recurrence 71_ Unomi
nacos-高可用seata之TC搭建(02)
Remember an error in MySQL: the user specified as a definer ('mysql.infoschema '@' localhost ') does not exist
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
05. 博客项目之安全
Vulhub vulnerability recurrence 73_ Webmin
随机推荐
Promotion hung up! The leader said it wasn't my poor skills
js Array 列表 实战使用总结
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
Implementing fuzzy query with dataframe
Pix2pix: image to image conversion using conditional countermeasure networks
Questions d'examen écrit classiques du pointeur
Easy to understand IIC protocol explanation
01. 开发博客项目之项目介绍
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
03. Login of development blog project
Three.js学习-光照和阴影(了解向)
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
改善Jpopup以实现动态控制disable
flutter 实现一个有加载动画的按钮(loadingButton)
MySQL if and ifnull use
Simple understanding of interpreters and compilers
算法-- 爬楼梯(Kotlin)
[machine learning notes] univariate linear regression principle, formula and code implementation
05. Security of blog project
Configuration file converted from Excel to Lua