415 Add Strings
Given two non-negative integersnum1
andnum2
represented as string, return the sum ofnum1
andnum2
.
Note:
The length of both
num1
andnum2
is < 5100.Both
num1
andnum2
contains only digits0-9
.Both
num1
andnum2
does not contain any leading zero.You must not use any built-in BigInteger library or convert the inputs to integer directly.
public String addStrings(String num1, String num2) {
if (num1 == null || num2 == null || num1.isEmpty() || num2.isEmpty()) {
return "0";
}
int n1 = num1.length();
int n2 = num2.length();
int i = n1 - 1;
int j = n2 - 1;
int carry = 0;
StringBuilder res = new StringBuilder();
while (i >= 0 || j >= 0) {
int dig1 = i >= 0 ? Character.getNumericValue(num1.charAt(i)) : 0;
int dig2 = j >= 0 ? Character.getNumericValue(num2.charAt(j)) : 0;
int sum = dig1 + dig2 + carry;
res.append(sum % 10);
carry = sum / 10;
i--;
j--;
}
if (carry == 1) {
res.append(1);
}
return res.reverse().toString();
}
Last updated
Was this helpful?