import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算两个数之和 * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */ public String solve (String s, String t) { // write code here if (s == null || t == null) { return s != null ? s : t; } char[] sArr = s.toCharArray(); char[] tArr = t.toCharArray(); int sLen = sArr.length; int tLen = tArr.length; int maxLen = Math.max(sLen, tLen); int i = sLen - 1; int j = tLen - 1; int cur = 0; int pre = 0; List<Integer> list = new LinkedList<>(); for (int k = 0; k < maxLen; k++) { int first = i >= 0 ? (int) (sArr[i] - '0') : 0; int second = j >= 0 ? (int) (tArr[j] - '0') : 0; int sum = first + second + pre; cur = sum % 10; pre = sum / 10; list.add(cur); i--; j--; } if (pre > 0) { list.add(pre); } StringBuilder sb = new StringBuilder(); Collections.reverse(list); for (int num : list) { sb.append(num); } return sb.toString(); } }