题解 | #大数加法#
大数加法
http://www.nowcoder.com/practice/11ae12e8c6fe48f883cad618c2e81475
java版
public class Solution {
public String solve (String s, String t) {
int i = s.length() - 1;
int j = t.length() - 1;
int curr = 0;
int carry = 0;
StringBuffer sb = new StringBuffer();
while(i >= 0 && j >= 0){
int a = s.charAt(i) - '0';
int b = t.charAt(j) - '0';
curr = (a + b + carry) % 10;
carry = (a + b + carry) / 10;
sb.insert(0, curr);
i--;
j--;
}
while(i >= 0){
int a = s.charAt(i) - '0';
curr = (a + carry) % 10;
carry = (a + carry) / 10;
sb.insert(0, curr);
i--;
}
while(j >= 0){
int b = t.charAt(j) - '0';
curr = (b + carry) % 10;
carry = (b + carry) / 10;
sb.insert(0, curr);
j--;
}
if(carry > 0){
sb.insert(0, carry);
}
return sb.toString();
}
}