分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

43. Multiply Strings(js)

发布时间:2023-09-06 02:33责任编辑:彭小芳关键词:js

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;};

43. Multiply Strings(js)

原文地址:https://www.cnblogs.com/xingguozhiming/p/10424924.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved