You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
function getMultiply(str1,str2){
let total = 0
let count = 0
for (let i =str1.length-1;i>=0;i--){
let c = (str1.charAt(i))*1
let s2 = str2*1
let res = c*s2*Math.pow(10,count)
count++
total+=res
}
return total
}
console.log(getMultiply('2','3'))
console.log(getMultiply("123","456"))
Activity
marlboroKay commentedon Sep 8, 2020
sisterAn commentedon Sep 9, 2020
解法一:常规解法
从右往左遍历乘数,将乘数的每一位与被乘数相乘得到对应的结果,再将每次得到的结果累加
另外,当乘数的每一位与被乘数高位(非最低位)相乘的时候,注意低位补
'0'复杂度分析:
解法二:竖式相乘(优化)
两个数M和N相乘的结果可以由 M 乘上 N 的每一位数的和得到 ,如下图所示:
num1依次乘上num2的每一位的和num1 * num2的结果复杂度分析:
leetcode
Ycheck521 commentedon Sep 9, 2020
JohnApache commentedon Apr 28, 2021
没想到特别好的方法,只能用两个
for, 这个和相加,相乘和相加不一样,AlexZhang11 commentedon May 31, 2024
function getMultiply(str1,str2){ let total = 0 let count = 0 for (let i =str1.length-1;i>=0;i--){ let c = (str1.charAt(i))*1 let s2 = str2*1 let res = c*s2*Math.pow(10,count) count++ total+=res } return total } console.log(getMultiply('2','3')) console.log(getMultiply("123","456"))