Dados dos enteros no negativos num1 y num2 expresados en forma de cadena, devuelve el producto de num1 y num2, y su producto también se expresa en forma de cadena.
Ejemplo 1:
Entrada: num1 = "2", num2 = "3"
Salida: "6"
Ejemplo 2:
Entrada: num1 = "123", num2 = "456"
Salida: "56088"
Descripción:
La longitud de num1 y num2 es menor que 110.
num1 y num2 solo contienen los números 0-9.
Ni num1 ni num2 comienzan con un cero, a menos que sea el mismo número 0.
No puede utilizar ningún tipo de gran número de la biblioteca estándar (como BigInteger) o convertir directamente la entrada en un número entero para su procesamiento.
Fuente: LeetCode Enlace: https://leetcode-cn.com/problems/multiply-strings
Método 1: vertical ordinario
public static String multiply1(String num1, String num2) {
//1.如果num1或num2为"0"
if (num1.equals("0") || num2.equals("0")) return "0";
//2.不为"0"的情况下,把num2的每一位与num1相乘
//保存计算结果
String result = "";
for (int i = num2.length() - 1; i >= 0; i--) {
StringBuilder str = new StringBuilder();
//2.1除了num2第一位,后面每一位与都需要补零
for (int j = 0; j < num2.length() - 1 - i; j++) {
str.append('0');
}
int carry = 0;
int n2 = num2.charAt(i) - '0';
// num2 的第 i 位数字 n2 与 num1 相乘
for (int j = num1.length() - 1; j >= 0; j--) {
int n1 = num1.charAt(j) - '0';
int tmp = n2 * n1 + carry;
carry = tmp / 10;
str.append((char) (tmp % 10 + '0'));
}
if (carry != 0) str.append((char) (carry + '0'));
// 将当前结果与新计算的结果求和作为新的结果
result = addStrings(result, str.reverse().toString());
}
return result;
}
//字符串相加
public static String addStrings(String num1, String num2) {
StringBuilder str = new StringBuilder();
int i = num1.length() - 1;
int j = num2.length() - 1;
int carry = 0, tmp;
while (i >= 0 || j >= 0) {
int n1 = i >= 0 ? num1.charAt(i) - '0' : 0;
int n2 = j >= 0 ? num2.charAt(j) - '0' : 0;
tmp = n1 + n2 + carry;
if (tmp / 10 == 1) carry = 1;
else carry = 0;
str.append((char) (tmp % 10 + '0'));
i--;
j--;
}
if (carry == 1) str.append('1');
return str.reverse().toString();
}
Método 2: optimizar la vertical
El algoritmo se completa con la ley de multiplicar dos números, multiplicar un cierto dígito del multiplicador por un cierto dígito del multiplicando y producir el resultado. Las reglas específicas son las siguientes: El número de dígitos en el multiplicador num1 es MM, y el
número de dígitos en el multiplicando num2 es NN, y el resultado de num1 x num2 es M + N. El resultado de num1 [i] x num2 [j] es
tmp (número de dígitos) Dos dígitos, en forma de "0x", "xy"), el primer dígito es res [i + j] y el segundo dígito es res [i + j + 1 ].
public static String multiply2(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) return "0";
int[] res = new int[num1.length() + num2.length()];
for (int i = 0; i < num1.length(); i++) {
int n1 = num1.charAt(i) - '0';
for (int j = 0; j < num2.length(); j++) {
int n2 = num2.charAt(j) - '0';
int tmp = n1 * n2;
res[i + j] += tmp / 10;
res[i + j + 1] += tmp % 10;
}
}
int carry = 0;
StringBuilder str = new StringBuilder();
for (int i = num1.length() + num2.length() - 1; i >= 0; i--) {
if (i == 0 && res[i] == 0) continue;
str.append((char) (res[i] + carry) % 10);
carry = (res[i] + carry) / 10;
}
return str.toString();
}