当前位置:网站首页>leetcode-43. String multiplication
leetcode-43. String multiplication
2022-07-03 23:23:00 【Qianfan at the front】
subject
It is strongly recommended to check the reference materials for this question
Code
/** * @param {string} num1 * @param {string} num2 * @return {string} */
// Analog multiplication
var multiply = function(num1, num2) {
let len1 = num1.length;
let len2 = num2.length;
let res = new Array(len1 + len2).fill(0);
// Double traversal analog multiplication
for (let i = len1-1; i >= 0; i--) {
for (let j = len2-1; j >= 0; j--) {
// Analog addition
let p1 = i+j;
let p2 = i+j+1;
let mul = num1[i] * num2[j];
let sum = mul + res[p2];
res[p2] = sum%10;
res[p1] += parseInt(sum/10);
}
}
// The character on the left of the array may be 0
let i = 0;
while(res[i] == '0') i++;
// In the array, except for the front 0 The characters of , Other characters form a string
let str = '';
for (; i<res.length; i++) {
str += res[i];
}
// Leading the array to zero , And form a string
// let str = '';
// let hasZero = true;
// for (let i = 0; i < res.length; i++) {
// if (res[i] != '0') {
// hasZero = false;
// }
// if (!hasZero) {
// str += res[i];
// }
// }
return str.length === 0 ? '0' : str;
};
Reference material
- [ String multiplication calculation (https://labuladong.gitee.io/algo/4/32/135/)
边栏推荐
- Leetcode week 4: maximum sum of arrays (shape pressing DP bit operation)
- Selenium check box
- SDMU OJ#P19. Stock trading
- [untitled]
- In 2022, 6G development has indeed warmed up
- Amway by head has this project management tool to improve productivity in a straight line
- EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
- How about opening an account at Hengtai securities? Is it safe?
- [15th issue] Tencent PCG background development internship I, II and III (OC)
- C3p0 connection MySQL 8.0.11 configuration problem
猜你喜欢
Scratch uses runner Py run or debug crawler
Current detection circuit - including op amp current scheme
The difference between single power amplifier and dual power amplifier
Learning notes of raspberry pie 4B - IO communication (SPI)
Summary of fluent systemchrome
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120
Programming language (2)
Overview of Yunxi database executor
2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) simulated examination and Guangdong Provincial Safety Officer a certificate third batch (main person in charg
随机推荐
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Bufferpool caching mechanism for executing SQL in MySQL
The first game of the new year, many bug awards submitted
Qtoolbutton available signal
[15th issue] Tencent PCG background development internship I, II and III (OC)
Day30-t540-2022-02-14-don't answer by yourself
Shiftvit uses the precision of swing transformer to outperform the speed of RESNET, and discusses that the success of Vit does not lie in attention!
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
Introduction to the gtid mode of MySQL master-slave replication
Design of logic level conversion in high speed circuit
Arc135 partial solution
Classification and extension of OC
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Pan Yueming helps Germany's Rochester Zodiac custom wristwatch
webAssembly
炒股开户佣金优惠怎么才能获得,网上开户安全吗
Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
Simple solution of m3u8 file format
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
How to switch between dual graphics cards of notebook computer