模拟加法操作,使用StringBuilder存储结果
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 计算两个数之和
* @param s string字符串 表示第一个整数
* @param t string字符串 表示第二个整数
* @return string字符串
*/
public String solve (String s, String t) {
// 获得两个字符串最后一个字符下标
int ls = s.length()-1;
int lt = t.length()-1;
int carry = 0; //存储进位
StringBuilder res = new StringBuilder();
while(ls>=0&<>=0){
int tempS = s.charAt(ls)-'0';
int tempT = t.charAt(lt)-'0';
int tempRes = tempS + tempT+carry;
res.append(tempRes%10);
carry = tempRes/10;
ls--;
lt--;
}
while(ls>=0){
int tempS = s.charAt(ls)-'0';
int tempRes = tempS + carry;
res.append(tempRes%10);
carry = tempRes/10;
ls--;
}
while(lt>=0){
int tempT = t.charAt(lt)-'0';
int tempRes = tempT+carry;
res.append(tempRes%10);
carry = tempRes/10;
lt--;
}
if(carry != 0){
res.append(carry);
}
return res.reverse().toString();
}
}