题解 | #大数加法#
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
package com.hhdd; /** * @Author huanghedidi * @Date 2022/8/23 22:24 */ public class 大数加法 { public static void main(String[] args) { String res = solve("12315435", "4566876"); System.out.println("res = " + res); } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 计算两个数之和 * * @param s string字符串 表示第一个整数 * @param t string字符串 表示第二个整数 * @return string字符串 */ public static String solve(String s, String t) { // write code here int cur = 0; int jinwei = 0; s = new StringBuilder(s).reverse().toString(); t = new StringBuilder(t).reverse().toString(); StringBuilder sb = new StringBuilder(); while (cur < s.length() && cur < t.length()) { int i1 = Integer.parseInt(s.charAt(cur) + ""); int i2 = Integer.parseInt(t.charAt(cur) + ""); int tmp = i1 + i2 + jinwei; jinwei = tmp / 10; sb.append(tmp % 10); cur++; } while (cur < s.length()) { int i1 = Integer.parseInt(s.charAt(cur) + ""); int tmp = i1 + jinwei; jinwei = tmp / 10; sb.append(tmp % 10); cur++; } while (cur < t.length()) { int i2 = Integer.parseInt(t.charAt(cur) + ""); int tmp = i2 + jinwei; jinwei = tmp / 10; sb.append(tmp % 10); cur++; } if (jinwei != 0){ sb.append(jinwei); } return sb.reverse().toString(); } }