题解 | #大数加法#
大数加法
https://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
using System;
using System.Collections.Generic;
using System.Text;
class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 计算两个数之和
* @param s string字符串 表示第一个整数
* @param t string字符串 表示第二个整数
* @return string字符串
*/
public string solve (string s, string t) {
// write code here
if (t.Length == 0) return s;
if (s.Length == 0) return t;
if (s.Length < t.Length) {
string temp = "";
temp = s;
s = t;
t = temp;
}
StringBuilder sb = new StringBuilder();
int carry = 0;
for (int i = s.Length - 1; i >= 0 ; i--) {
int temp = s[i] - '0' + carry;
int j = i - s.Length + t.Length;
if (j >= 0)
temp = t[j] - '0' + temp;
carry = temp / 10;
temp %= 10;
sb.Append((char)(temp + '0'));
}
if (carry == 1) sb.Append('1');
StringBuilder res = new StringBuilder();
for (int i = sb.Length - 1; i >= 0 ; i--) {
res.Append(sb[i]);
}
return res.ToString();
}
}

