当前位置:网站首页>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/)
边栏推荐
- Enter MySQL in docker container by command under Linux
- Learning methods of zynq
- Les sociétés de valeurs mobilières dont la Commission d'ouverture d'un compte d'actions est la plus faible ont ce que tout le monde recommande.
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- How about opening an account at Hengtai securities? Is it safe?
- finalize finalization finally final
- Minimum commission for stock account opening. Stock account opening is free. Is online account opening safe
- Hcip day 14 notes
- Label coco format data and format data in the upper left corner and lower right corner are mutually converted
- X Opencv feature point detection and matching
猜你喜欢

2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination

Overview of Yunxi database executor

How to connect a laptop to a projector

Summary of fluent systemchrome

2022 t elevator repair registration examination and the latest analysis of T elevator repair

2/14 (regular expression, sed streaming editor)

Recursive least square adjustment

How to restore the factory settings of HP computer

EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance

How to make icons easily
随机推荐
D28:maximum sum (maximum sum, translation)
Is the controller a single instance or multiple instances? How to ensure the safety of concurrency
Interesting 10 CMD commands
How to restore the factory settings of HP computer
Fluent learning (5) GridView
Fashion cloud interview questions series - JS high-frequency handwritten code questions
Pointer concept & character pointer & pointer array yyds dry inventory
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
[Android reverse] use the DB browser to view and modify the SQLite database (copy the database file from the Android application data directory | use the DB browser tool to view the data block file)
Maxwell equation and Euler formula - link
Comment obtenir une commission préférentielle pour l'ouverture d'un compte en bourse? Est - ce que l'ouverture d'un compte en ligne est sécurisée?
Apple released a supplementary update to MacOS Catalina 10.15.5, which mainly fixes security vulnerabilities
File copy method
Pat grade A - 1164 good in C (20 points)
What are the common computer problems and solutions
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
Hcip day 16 notes
Creation of the template of the password management software keepassdx
Yyds dry goods inventory [practical] simply encapsulate JS cycle with FP idea~
股票开户佣金最低的券商有哪些大家推荐一下,手机上开户安全吗