第一行是j,第二行是i
class Solution {
public String multiply(String num1, String num2) {
if (num1.equals("0") || num2.equals("0")) {
return "0";
}
int length1 = num1.length();
int length2 = num2.length();
int[] results = new int[length1 + length2];
for (int i = length1 - 1; i >= 0; --i) {
int x = num1.charAt(i) - '0';
for (int j = length2 - 1; j >= 0; --j) {
int y = num2.charAt(j) - '0';
int sum = results[i + j + 1] + x * y;
results[i + j + 1] = sum % 10;
//每一次的结果都得加上前一位的结果
results[i + j] += sum /10;
}
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < results.length; ++i) {
if (i == 0 && results[i] == 0) continue;
stringBuilder.append(results[i]);
}
return stringBuilder.toString();
}
}
来源:https://leetcode-cn.com/problems/multiply-strings/solution/you-hua-ban-shu-shi-da-bai-994-by-breezean/