43. Multiply Strings
Given two non-negative integers num1
and num2
represented as strings, return the product of num1
and num2
, also represented as a string.
Example 1:
Input: num1 = "2", num2 = "3" Output: "6"
Example 2:
Input: num1 = "123", num2 = "456" Output: "56088"题意:实现‘乘’运算符代码如下:
/** * @param {string} num1 * @param {string} num2 * @return {string} */ var multiply = function(num1, num2) { let m=num1.length,n=num2.length; let pos=new Array(m+n).fill(0); for(let i=m-1;i>=0;i--){ for(let j=n-1;j>=0;j--){ let mul=parseInt(num1.charAt(i))*parseInt(num2.charAt(j)); let p1=i+j,p2=i+j+1; let sum=mul+pos[p2]; pos[p1]+=parseInt(sum/10); pos[p2]=sum%10; } } let sb=‘‘; for(let p of pos){ if(!(p==0 && sb.length==0)) sb+=p; } return sb.length==0?"0":sb; };
原文地址:https://www.cnblogs.com/xingguozhiming/p/10424924.html
时间: 2024-10-07 23:58:04