Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

javascript - Javascript乘字符串(Javascript multiplying strings)

i was doing following leetcode question on multiplying strings: https://leetcode.com/problems/multiply-strings/(我在做关于字符串相乘的leetcode问题: https ://leetcode.com/problems/multiply-strings/)

The question is(问题是) Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.(给定两个非负整数num1和num2表示为字符串,返回num1和num2的乘积,也表示为字符串。) Example 1:(范例1:) Input: num1 = "2", num2 = "3" Output: "6" Example 2: Input: num1 = "123", num2 = "456" Output: "56088" Note:(注意:) The length of both num1 and num2 is < 110. Both num1 and num2 contain(num1和num2的长度均小于110。num1和num2均包含) only digits 0-9.(仅数字0-9。) Both num1 and num2 do not contain any leading zero,(num1和num2都不包含任何前导零,) except the number 0 itself.(除了数字0本身。) You must not use any built-in BigInteger(您不得使用任何内置的BigInteger) library or convert the inputs to integer directly.(库或将输入直接转换为整数。) For this used the following approach 1. Convert the string to int 2. Multiply the int(为此,使用以下方法:1.将字符串转换为int 2.将int乘以) Algo for the same is(同样的算法是) const numberMap = { "0": 0, "1": 1, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9 } var multiply = function(num1, num2) { let i = num1.length let j = num2.length let sum = currentPower = 0 let firstNumber = secondNumber = 0 while(i > 0 || j > 0) { // if I or J is equal to zero, means we have itterated hence we will set the value to one const firstNum = i > 0 ? (numberMap[num1[i-1]]) * (10**currentPower) : 0 const secondNum = j > 0 ? (numberMap[num2[j-1]]) * (10**currentPower) : 0 firstNumber += firstNum secondNumber += secondNum currentPower++ i-- j-- } sum = firstNumber * secondNumber return sum.toString() }; but when the following input is given(但是当给出以下输入时) "123456789" "987654321" it yields the following output "121932631112635260" instead of "121932631112635269"(它产生以下输出"121932631112635260"而不是"121932631112635269") Any idea how I can fix this?(知道我该如何解决吗?)   ask by anny123 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You could multiply each digit with each other digit and take the index ad postion.(您可以将每个数字彼此相乘并取索引位置。)

This approach uses the reversed arrays of the strings and reverse the result set as well.(这种方法使用字符串的反向数组,并且也反转结果集。) function multiply(a, b) { var aa = [...a].reverse(), bb = [...b].reverse(), p = [], i, j; for (i = 0; i < aa.length; i++) { for (j = 0; j < bb.length; j++) { if (!p[i + j]) p[i + j] = 0; p[i + j] += aa[i] * bb[j]; if (p[i + j] > 9) { if (!p[i + j + 1]) p[i + j + 1] = 0; p[i + j + 1] += Math.floor(p[i + j] / 10); p[i + j] %= 10; } } } return p.reverse().join(''); } console.log(multiply('2', '3')); // 6 console.log(multiply('123', '456')); // 56088

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...