# 日常记录——leetcode- 字符串相加

num1 和num2 的长度都小于 5100
num1 和num2 都只包含数字 0-9
num1 和num2 都不包含任何前导零

public String addStrings(String num1, String num2) {
StringBuilder result = new StringBuilder();
StringBuilder sa = new StringBuilder(num1);
sa = sa.reverse();
StringBuilder sc = new StringBuilder(num2);
sc = sc.reverse();
int length = num1.length()>num2.length()?num1.length():num2.length();
//追加位数
if(num1.length()<length){
for (int i = 0; i < length-num1.length(); i++) {
sa.append("0");
}
}
if(num2.length()<length){
for (int i = 0; i < length-num2.length(); i++) {
sc.append("0");
}
}
//记录数值加和
int sum = 0;
for (int i = 0; i < length; i++) {
sum = (sa.charAt(i)-'0')+(sc.charAt(i)-'0');
//进位判断  及 处理
if(sum>=10){
sum = sum%10;
if(sa.length()>=sc.length()){
if(sa.length()<=i+1){
//超出范围 直接最后一位进位  返回
result.append(sum);
result.append(1);
break;
}else{
sa.setCharAt(i+1,(char)(sa.charAt(i+1)+1));
}
}else{
if(sc.length()<=i+1){
result.append(sum);
result.append(1);
break;
}else{
sc.setCharAt(i+1,(char)(sa.charAt(i+1)+1));
}
}
}
result.append(sum);
}
return result.reverse().toString();
}


 public String addStrings(String num1, String num2) {
StringBuilder result = new StringBuilder();、
//倒叙
int s1index = num1.length()-1;
int s2index = num2.length()-1;
//记录数值加和
int sum = 0;
while(s1index >= 0 || s2index >= 0 || sum!=0){
if(s1index >= 0){
sum += num1.charAt(s1index)-'0';
s1index--;
}
if(s2index >= 0){
sum += num2.charAt(s2index)-'0';
s2index--;
}
//直接取模10
result.append(sum%10);
//将进位加到下次里
sum = sum/10;
}
return result.reverse().toString();
}


09-25 171
05-03 194
12-29 35
09-16 253
05-28 176
11-13 105