-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
43. Multiply Strings #296
Comments
Solution
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
var multiply = function(num1, num2) {
return sum(digitMultiply(num1, num2));
function digitMultiply(num1, num2) {
num2 = '0' + num2;
const digitProducts = [];
for(let i = num1.length - 1; i >= 0; i--) {
let curry = 0;
let digitProduct = ''.padEnd(num1.length - 1 - i, '0');
for (let j = num2.length - 1; j >= 0; j--) {
const product = Number(num1[i]) * Number(num2[j]) + curry;
digitProduct = String(product % 10) + digitProduct;
curry = Math.floor(product / 10);
}
digitProducts.push(digitProduct);
}
return digitProducts;
}
function sum(nums) {
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
let curry = 0;
let digitSum = '';
const len = result.length > nums[i].length ? result.length + 1 : nums[i].length + 1;
result = result.padStart(len, '0');
const num = nums[i].padStart(len, '0');
for (let j = len - 1; j >= 0; j--) {
digitSum = String((Number(result[j]) + Number(num[j]) + curry) % 10) + digitSum;
curry = Math.floor((Number(result[j]) + Number(num[j]) + curry) / 10);
}
result = digitSum;
}
let separator = 0;
while(separator !== result.length - 1 && result[separator] === '0') {
separator += 1;
}
return result.slice(separator);
}
};
function multiply(num1: string, num2: string): string {
return sum(digitMultiply(num1, num2));
function digitMultiply(num1: string, num2: string): string[] {
num2 = '0' + num2;
const digitProducts: string[] = [];
for(let i = num1.length - 1; i >= 0; i--) {
let curry = 0;
let digitProduct = ''.padEnd(num1.length - 1 - i, '0');
for (let j = num2.length - 1; j >= 0; j--) {
const product = Number(num1[i]) * Number(num2[j]) + curry;
digitProduct = String(product % 10) + digitProduct;
curry = Math.floor(product / 10);
}
digitProducts.push(digitProduct);
}
return digitProducts;
}
function sum(nums: string[]): string {
let result = nums[0];
for (let i = 1; i < nums.length; i++) {
let curry = 0;
let digitSum = '';
const len = result.length > nums[i].length ? result.length + 1 : nums[i].length + 1;
result = result.padStart(len, '0');
const num = nums[i].padStart(len, '0');
for (let j = len - 1; j >= 0; j--) {
digitSum = String((Number(result[j]) + Number(num[j]) + curry) % 10) + digitSum;
curry = Math.floor((Number(result[j]) + Number(num[j]) + curry) / 10);
}
result = digitSum;
}
let separator = 0;
while(separator !== result.length - 1 && result[separator] === '0') {
separator += 1;
}
return result.slice(separator);
}
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
43. Multiply Strings
给定两个以字符串形式表示的非负整数
num1
和num2
,返回num1
和num2
的乘积,它们的乘积也表示为字符串形式。Example 1
Example 2
Note
num1
和num2
的长度小于110。num1
和num2
只包含数字0-9
。num1
和num2
均不以零开头,除非是数字 0 本身。The text was updated successfully, but these errors were encountered: