当前位置:网站首页>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/)
边栏推荐
- . Net ADO splicing SQL statement with parameters
- 炒股开户佣金优惠怎么才能获得,网上开户安全吗
- Common mode interference of EMC
- D26: the nearest number (translation + solution)
- C3p0 connection MySQL 8.0.11 configuration problem
- What are the common computer problems and solutions
- 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!
- Pat grade A - 1164 good in C (20 points)
- C # basic knowledge (1)
- Recursion and recursion
猜你喜欢
![[automation operation and maintenance novice village] flask-2 certification](/img/9a/a9b45e1f41b9b75695dcb06c212a69.jpg)
[automation operation and maintenance novice village] flask-2 certification
Creation of the template of the password management software keepassdx

How to switch between dual graphics cards of notebook computer

QT creator source code learning note 05, how does the menu bar realize plug-in?

Bufferpool caching mechanism for executing SQL in MySQL

Take you to master the formatter of visual studio code

SDMU OJ#P19. Stock trading

To rotate 90 degrees clockwise and modify the video format

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

MLX90614 driver, function introduction and PEC verification
随机推荐
Interesting 10 CMD commands
How to quickly build high availability of service discovery
Label coco format data and format data in the upper left corner and lower right corner are mutually converted
Enter MySQL in docker container by command under Linux
2022 a special equipment related management (elevator) examination questions and a special equipment related management (elevator) examination contents
Pandaoxi's video
[network security] what is emergency response? What indicators should you pay attention to in emergency response?
Programming language (1)
Simple solution of m3u8 file format
在恒泰证券开户怎么样?安全吗?
Bufferpool caching mechanism for executing SQL in MySQL
[untitled]
How to solve win10 black screen with only mouse arrow
Qtoolbutton - menu and popup mode
C summary of knowledge point definitions, summary notes
Loop compensation - explanation and calculation of first-order, second-order and op amp compensation
Mongoose the table associated with the primary key, and automatically bring out the data of another table
Blue Bridge Cup -- Mason prime
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
Analysis of refrigeration and air conditioning equipment operation in 2022 and examination question bank of refrigeration and air conditioning equipment operation