43. Multiply Strings (Medium)

https://leetcode.com/problems/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"

Note:

  1. The length of both num1 and num2 is < 110.
  2. Both num1 and num2 contain only digits 0-9.
  3. Both num1 and num2 do not contain any leading zero, except the number 0 itself.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

Hints

Solutions

class Solution {
    public String multiply(String num1, String num2) {
        if (num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0) { return "0"; }
        int n = num1.length();
        int m = num2.length();
        char[] n1 = num1.toCharArray();
        char[] n2 = num2.toCharArray();
        int[] ans = new int[n + m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[n - 1 + m - 1 - (i + j)] += (n1[i] - '0') * (n2[j] - '0');
            }
        }
        for (int i = 0; i < n + m; i++) {
            if (i < n + m - 1) {
                ans[i + 1] = ans[i + 1] + (ans[i] / 10);
            }
            ans[i] = ans[i] % 10;
        }
        StringBuilder sb = new StringBuilder();
        int i = n + m - 1;
        while (i >= 0 && ans[i] == 0) { i--; }
        for (int j = i; j >= 0; j--) { sb.append(ans[j]); }
        return sb.length() == 0 ? "0" : sb.toString();
    }
}
Copyright © iovi.com 2017 all right reserved,powered by GitbookLast Modification: 2019-04-15 18:32:07

results matching ""

    No results matching ""