categories: [Blog,Algorithm]
43. 字符串相乘
难度中等574
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。
示例 1:
输入: num1 = “2”, num2 = “3”
输出: “6”
示例 2:
输入: num1 = “123”, num2 = “456”
输出: “56088”
说明:
num1和num2的长度小于110。num1和num2只包含数字0-9。num1和num2均不以零开头,除非是数字 0 本身。- 不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。
class Solution {public String multiply(String num1, String num2) {if (num1.equals("0") || num2.equals("0")) {return "0";}int m = num1.length(), n = num2.length();int[] ansArr = new int[m + n];//最多位数for (int i = m - 1; i >= 0; i--) {int x = num1.charAt(i) - '0';//字母转数字 倒序for (int j = n - 1; j >= 0; j--) {int y = num2.charAt(j) - '0';ansArr[i + j + 1] += x * y;}}for (int i = m + n - 1; i > 0; i--) {ansArr[i - 1] += ansArr[i] / 10;ansArr[i] %= 10;//进位处理}int index = ansArr[0] == 0 ? 1 : 0;//去掉前缀0StringBuffer ans = new StringBuffer();while (index < m + n) {ans.append(ansArr[index]);index++;}return ans.toString();}}
