问题原文https://leetcode-cn.com/problems/multiply-strings/description/
//任意一个整数和长度为1的整数相乘
public String helpFun(String x,String y){
StringBuilder res = new StringBuilder();
int len = x.length();
Stack<Integer> stack = new Stack<Integer>();
for(int i = len-1;i >= 0;i--){
int tmp = (x.charAt(i)-48)*(y.charAt(0)-48);
if (!stack.isEmpty()) {
tmp+=stack.pop();
}
if(tmp <= 9) {
stack.add(tmp);
stack.add(0);
}
else {
stack.add(tmp % 10);
stack.add(tmp / 10);
}
}
if (!stack.isEmpty()&&stack.peek() == 0) stack.pop();
while(!stack.isEmpty()){
res.append(stack.pop());
}
return res.toString();
}
//任意两个整数相加
public String plus(String x ,String y){
StringBuilder res = new StringBuilder();
Stack<Integer> stack = new Stack<Integer>();
int len1 = x.length();
int len2 = y.length();
int i = 0;
for (;i<len1&&i<len2;i++){
int tmp = (x.charAt(len1-i-1)-48)+(y.charAt(len2-i-1)-48);
if (!stack.isEmpty()) tmp+=stack.pop();
if (tmp>9){
stack.add(tmp%10);
stack.add(tmp/10);
}else{
stack.add(tmp);
stack.add(0);
}
}
while(i < len1){
int tmp = x.charAt(len1-i-1)-48;
if (!stack.isEmpty()) tmp+=stack.pop();
if (tmp>9){
stack.add(tmp%10);
stack.add(tmp/10);
}else{
stack.add(tmp);
stack.add(0);
}
i++;
}
while(i < len2){
int tmp = y.charAt(len2-i-1)-48;
if (!stack.isEmpty()) tmp+=stack.pop();
if (tmp>9){
stack.add(tmp%10);
stack.add(tmp/10);
}else{
stack.add(tmp);
stack.add(0);
}
i++;
}
if (stack.peek() == 0) stack.pop();
while(!stack.isEmpty()){
res.append(stack.pop());
}
return res.toString();
}
public String multiply(String num1, String num2) {
if(num1.equals("0") || num2.equals("0")) return "0";
int len = num2.length();
String res = "0";
for (int i = len-1;i >= 0;i--){
String tmp = helpFun(num1,String.valueOf(num2.charAt(i)));
for (int j = 0;j<len-1-i;j++)
tmp+="0";
res = plus(res,tmp);
}
return res;
}