public class Solution {
    public String multiply(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();

        int[] product = new int[len1 + len2];

        for (int i = len1 - 1; i >= 0; --i) {
            for (int j = len2 - 1; j >= 0; --j) {
                int digit1 = num1.charAt(i) - '0';
                int digit2 = num2.charAt(j) - '0';
                product[i + j + 1] += digit1 * digit2;
            }
        }

        int carry = 0;
        for (int i = product.length - 1; i >= 0; --i) {
            int num = product[i] + carry;
            product[i] = num % 10;
            carry = num / 10;
        }

        StringBuilder sb = new StringBuilder();
        int index = 0;

        while (index < product.length && product[index] == 0) index++;
        while (index < product.length) {
            sb.append(product[index++]);
        }

        return sb.length() == 0 ? "0" : sb.toString();
    }
}

results matching ""

    No results matching ""