当前位置:网站首页>[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 !
边栏推荐
- [mask requirements of OSPF and Isis in multi access network]
- JS array list actual use summary
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- Promotion hung up! The leader said it wasn't my poor skills
- Steady, 35K, byte business data analysis post
- Select knowledge points of structure
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- Note the various data set acquisition methods of jvxetable
- MySQL advanced learning summary 9: create index, delete index, descending index, and hide index
- Promotion hung up! The leader said it wasn't my poor skills
猜你喜欢
Fiddler installed the certificate, or prompted that the certificate is invalid
Please wait while Jenkins is getting ready to work
C Advanced - data storage (Part 1)
Excel转换为Lua的配置文件
05. Security of blog project
Vulhub vulnerability recurrence 69_ Tiki Wiki
Questions d'examen écrit classiques du pointeur
Idea one key guide package
【torch】|torch.nn.utils.clip_grad_norm_
初识CDN
随机推荐
指針經典筆試題
Three.js学习-光照和阴影(了解向)
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
Vite configures the development environment and production environment
Vulhub vulnerability recurrence 72_ uWSGI
UCF(2022暑期团队赛一)
【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
剑指 Offer II 039. 直方图最大矩形面积
01. 开发博客项目之项目介绍
Deep learning -yolov5 introduction to actual combat click data set training
GAMES202-WebGL中shader的编译和连接(了解向)
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
Notes, continuation, escape and other symbols
Vulhub vulnerability recurrence 73_ Webmin
Cuda11.1 online installation
Idea one key guide package
Three. JS learning - light and shadow (understanding)
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Imperial cms7.5 imitation "D9 download station" software application download website source code
04. 项目博客之日志