import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算两个数之和 * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */ // cur_num:当前计算结果 // Bao:为保留位 // Jin:当前进位 //诸位计算: cur_num = s[i] + s[j] + Jin //从cur_num中分离出Bao和Jin public String solve (String s, String t) { if(s == null && t == null) return "0" ; if(s != null && s.length() == 0) return (t == null || t.length()== 0) ? "0" : t ; if(t != null && t.length() == 0) return (s == null || s.length() == 0) ? "0" : s ; char[] ss = s.toCharArray() , tt = t.toCharArray() ; StringBuilder st = new StringBuilder("") ; int slen = ss.length ,tlen = tt.length ; int i = slen - 1 , j = tlen - 1 ; int bao = 0 ;//当前保留位 int jin = 0 ;//当前进位 int cur_num = 0 ;//当前计算结果 while(i >= 0 && j >= 0) { cur_num = sum(ss[i] , tt[j]) + jin ; if(cur_num >= 10) { jin = 1 ; bao = cur_num - 10 ; } else { jin = 0 ; bao = cur_num ; } st.append((char)(bao+'0')) ; j -- ; i -- ; } while(i >= 0) { cur_num = sum(ss[i] , '0') + jin ; if(cur_num >= 10) { jin = 1 ; bao = cur_num - 10 ; } else { jin = 0 ; bao = cur_num ; } st.append((char)(bao+'0')) ; i -- ; } while(j >= 0) { cur_num = sum(tt[j] , '0') + jin ; if(cur_num >= 10) { jin = 1 ; bao = cur_num - 10 ; } else { jin = 0 ; bao = cur_num ; } st.append((char)(bao+'0')) ; j -- ; } if(jin != 0) { st.append('1') ; } return st.reverse().toString() ; } public int sum(char ch1 , char ch2) { return (int)(ch1-'0') + (int)(ch2-'0') ; } }