可以直接 char 赋值数字进行计算
但是我没那么做 相当于复习一遍 链表加法.
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算两个数之和 * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */ public String solve (String s, String t) { char [] numa = s.toCharArray(); char [] numb = t.toCharArray(); int i = numa.length-1; int j = numb.length-1; int ifadd = 0; char[] arr = new char[Math.max(i,j) + 1]; int k = arr.length-1; while(i>=0 || j>=0){ int res; if(i<0) {res = numb[j--]+ifadd-'0';} else if(j<0) {res = numa[i--]+ifadd-'0';} else {res = numa[i--] + numb[j--] + ifadd-'0'*2;} if(res >= 10){ res = res%10; ifadd = 1;} else ifadd = 0; arr[k--] = (char) (res+'0'); } if(ifadd == 1){ char[] arr2 = new char[arr.length+1]; for(int z = 0; z<arr.length ;z++){ arr2[z+1] = arr[z]; } arr2[0] = '1'; arr = arr2; } String str = new String(arr); return str; } }