package leetcode.simple;
public class no43_MultiplyStrings {
public static String multiply(String num1, String num2) {
if (num1 == null || num2 == null || num1.length() == 0 || num2.length() == 0) {
return "";
}
if(num1.equals("0")||num2.equals("0")){
return "0";
}
int len1 = num1.length();
int len2 = num2.length();
//数组长度为len1+len2-1
int[] res = new int[len1 + len2-1];
for (int i = len1 - 1; i >= 0; i--) {
for (int j = len2 - 1; j >= 0; j--) {
int tmp = (num1.charAt(i) - '0') * (num2.charAt(j) - '0');
res[i + j] += tmp;
}
}
StringBuilder sb = new StringBuilder();
int carry = 0;
for (int i = res.length-1; i >=0; i--) {
int digit = (res[i]+carry) % 10;
carry = (res[i]+carry) / 10;
sb.insert(0,digit);
}
if (carry > 0) {
sb.insert(0,carry);
}
return sb.length()==0?"0":sb.toString();
}
}
06-24
07-16
07-16
07-16
07-16
07-16
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交