当前位置:网站首页>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/)
边栏推荐
- 2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit
- How can I get the Commission discount of stock trading account opening? Is it safe to open an account online
- Introduction to the gtid mode of MySQL master-slave replication
- Recursion and recursion
- C # basic knowledge (3)
- 2/14 (regular expression, sed streaming editor)
- IDENTITY
- Hcip day 16 notes
- User login function: simple but difficult
- EPF: a fuzzy testing framework for network protocols based on evolution, protocol awareness and coverage guidance
猜你喜欢

Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?

Actual combat | use composite material 3 in application

Fluent learning (5) GridView

Amway by head has this project management tool to improve productivity in a straight line

Runtime. getRuntime(). totalMemory/maxMemory()

How to solve the "safe startup function prevents the operating system from starting" prompt when installing windows10 on parallel desktop?
![[Happy Valentine's day]](/img/d9/9280398eb64907a567df6eea772adb.jpg)
[Happy Valentine's day] "I still like you very much, like sin ² a+cos ² A consistent "(white code in the attached table)

Pan Yueming helps Germany's Rochester Zodiac custom wristwatch

Interpretation of corolla sub low configuration, three cylinder power configuration, CVT fuel saving and smooth, safety configuration is in place

Weekly leetcode - nc9/nc56/nc89/nc126/nc69/nc120
随机推荐
Enter MySQL in docker container by command under Linux
Go error collection | talk about the difference between the value type and pointer type of the method receiver
股票开户最低佣金炒股开户免费,网上开户安全吗
Qtoolbutton - menu and popup mode
X Opencv feature point detection and matching
Esp-idf turns off serial port log output.
Hcip day 12 notes
How to solve the problem of requiring a password when accessing your network neighborhood on your computer
Bufferpool caching mechanism for executing SQL in MySQL
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
[automation operation and maintenance novice village] flask-2 certification
The difference between single power amplifier and dual power amplifier
[untitled]
炒股开户佣金优惠怎么才能获得,网上开户安全吗
C # basic knowledge (3)
Design of logic level conversion in high speed circuit
How to understand the gain bandwidth product operational amplifier gain
Xiangong intelligent obtained hundreds of millions of yuan of b-round financing to accelerate the process of building non-standard solutions with standardized products
2022 chemical automation control instrument examination content and chemical automation control instrument simulation examination
2022 examination of safety production management personnel of hazardous chemical production units and examination skills of safety production management personnel of hazardous chemical production unit