题解 | #大数加法#
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
import java.util.*; public class Solution{ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算两个数之和 * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */ public static String solve (String s, String t) { if(s.isEmpty() || t.isEmpty()) return s.isEmpty() ? t : s; // 字符串反转 StringBuilder sb1 = new StringBuilder(new StringBuilder(s).reverse()); StringBuilder sb2 = new StringBuilder(new StringBuilder(t).reverse()); int len1 = sb1.length(); int len2 = sb2.length(); StringBuilder temp = new StringBuilder(); int cnt = 0, a, b; // 表示进位 int i = 0, j = 0; for(; i < len1 && j < len2; ++i, ++j){ a = sb1.charAt(i) - '0'; b = sb2.charAt(j) - '0'; temp.append((char)((a + b + cnt) % 10 + '0')); cnt = (a + b + cnt) / 10; } if(cnt != 0){ if(len1 == len2) temp.append((char)(cnt + '0')); else if(len1 < len2){ handler(sb2, len2, temp, cnt, j); } else { handler(sb1, len1, temp, cnt, i); } } else { if (len1 < len2) { temp.append(sb2.substring(j)); } else if (len1 > len2) { temp.append(sb1.substring(i)); } } return temp.reverse().toString(); } private static void handler(StringBuilder sb1, int len1, StringBuilder temp, int cnt, int i) { int a; while(i < len1){ a = sb1.charAt(i) - '0'; temp.append((char)((a + cnt) % 10 + '0')); cnt = (a + cnt) / 10; i++; } if(cnt != 0){ temp.append((char)(cnt + '0')); } } }