当前位置:网站首页>[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 !
边栏推荐
- Qt TCP 分包粘包的解决方法
- 【LeetCode】18、四数之和
- Fiddler installed the certificate, or prompted that the certificate is invalid
- 【云原生】3.1 Kubernetes平台安装KubeSpher
- Some common skills on unity inspector are generally used for editor extension or others
- F12 solve the problem that web pages cannot be copied
- Promise summary
- 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
- Select knowledge points of structure
- Sliding window problem review
猜你喜欢
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
February 12 relativelayout
05. Security of blog project
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
First acquaintance with CDN
自建DNS服务器,客户端打开网页慢,解决办法
【LeetCode】18、四数之和
JS quick start (II)
[leetcode16] the sum of the nearest three numbers (double pointer)
随机推荐
Vulhub vulnerability recurrence 72_ uWSGI
Mongodb basic knowledge summary
04. 项目博客之日志
Driver development - hellowdm driver
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
02. 开发博客项目之数据存储
[leetcode] 18. Sum of four numbers
Simple understanding of interpreters and compilers
02. Develop data storage of blog project
Force buckle 1189 Maximum number of "balloons"
初识CDN
01. 开发博客项目之项目介绍
Questions d'examen écrit classiques du pointeur
趋势前沿 | 达摩院语音 AI 最新技术大全
pix2pix:使用条件对抗网络的图像到图像转换
[effective Objective-C] - memory management
Yyds dry inventory SSH Remote Connection introduction
[detailed explanation of Huawei machine test] statistics of shooting competition results
C# AES对字符串进行加密
【torch】|torch.nn.utils.clip_grad_norm_