思路:
// @Title: 字符串相加 (Add Strings)
// @Author: qisiii
// @Date: 2024-05-10 16:04:46
// @Runtime: 1 ms
// @Memory: 41.7 MB
// @comment:
// @flag:
class Solution {
public String addStrings(String s, String t) {
if(s.length()==0||t.length()==0){
return s+t;
}
int max=Math.max(s.length(),t.length());
char[] result= new char[max+1];
int sl=s.length()-1;
int tl=t.length()-1;
int jin=0;
while(sl>=0||tl>=0){
int svalue=sl>=0?s.charAt(sl)-'0':0;
int tvalue=tl>=0?t.charAt(tl)-'0':0;
int cur=(svalue+tvalue+jin)%10;
jin=(svalue+tvalue+jin)/10;
result[max]=(char) (cur+'0');
sl--;tl--;max--;
}
result[max]=(char)(jin+'0');
return handleZero(result);
}
public String handleZero(char[] result){
int firstN0=0;
while(firstN0<result.length&&result[firstN0]=='0'){
firstN0++;
}
if(firstN0==result.length){
return "0";
}
return new String(result).substring(firstN0);
}
}