Skip to content
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

Open
Tcdian opened this issue Aug 13, 2020 · 1 comment
Open

43. Multiply Strings #296

Tcdian opened this issue Aug 13, 2020 · 1 comment

Comments

@Tcdian
Copy link
Owner

Tcdian commented Aug 13, 2020

43. Multiply Strings

给定两个以字符串形式表示的非负整数 num1num2,返回 num1num2 的乘积,它们的乘积也表示为字符串形式。

Example 1

Input: num1 = "2", num2 = "3"
Output: "6"

Example 2

Input: num1 = "123", num2 = "456"
Output: "56088"

Note

  • num1num2 的长度小于110。
  • num1num2 只包含数字 0-9
  • num1num2 均不以零开头,除非是数字 0 本身。
  • 不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。
@Tcdian
Copy link
Owner Author

Tcdian commented Aug 13, 2020

Solution

  • JavaScript 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);
    }
};
  • TypeScript Solution
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
Projects
None yet
Development

No branches or pull requests

1 participant