class Solution:
def addStrings(self, num1: str, num2: str) -> str:
i, j = len(num1) - 1, len(num2) - 1
carry = 0
res = []
while i >= 0 or j >= 0 or carry:
n1 = int(num1[i]) if i >= 0 else 0
n2 = int(num2[j]) if j >= 0 else 0
total = n1 + n2 + carry
res.append(str(total % 10))
carry = total // 10
i -= 1
j -= 1
return ''.join(res[::-1])
class Solution {
public:
string addStrings(string num1, string num2) {
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
string res = "";
while (i >= 0 || j >= 0 || carry) {
int n1 = i >= 0 ? num1[i--] - '0' : 0;
int n2 = j >= 0 ? num2[j--] - '0' : 0;
int sum = n1 + n2 + carry;
res.push_back(sum % 10 + '0');
carry = sum / 10;
}
reverse(res.begin(), res.end());
return res;
}
};
class Solution {
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
StringBuilder res = new StringBuilder();
while (i >= 0 || j >= 0 || carry != 0) {
int n1 = i >= 0 ? num1.charAt(i--) - '0' : 0;
int n2 = j >= 0 ? num2.charAt(j--) - '0' : 0;
int sum = n1 + n2 + carry;
res.append(sum % 10);
carry = sum / 10;
}
return res.reverse().toString();
}
}
var addStrings = function(num1, num2) {
let i = num1.length - 1, j = num2.length - 1, carry = 0, res = [];
while (i >= 0 || j >= 0 || carry) {
let n1 = i >= 0 ? +num1[i--] : 0;
let n2 = j >= 0 ? +num2[j--] : 0;
let sum = n1 + n2 + carry;
res.push(sum % 10);
carry = Math.floor(sum / 10);
}
return res.reverse().join('');
};
The "Add Strings" problem asks you to add two non-negative integers, num1
and num2
, where each integer is given as a non-empty string. You must return the sum as a string as well.
At first glance, you might think of simply converting the input strings to integers, adding them, and converting the result back to a string. However, the problem explicitly forbids this approach because the numbers can be much larger than what an integer type can hold.
Instead, you need to mimic the process of addition as you would do by hand: add the digits from right to left, keeping track of any carry. This is similar to how you would add two numbers on paper, starting from the units place and carrying over any overflow to the next digit.
The challenge is to handle different string lengths and manage the carry correctly throughout the addition.
i
and j
at the end of num1
and num2
respectively. Also, initialize a variable carry
to 0 and an empty result container (like a list or string builder).num1
and num2
(if the pointer is valid), otherwise use 0.(sum % 10)
, and update carry
as sum // 10
(for Python/Java/C++) or Math.floor(sum / 10)
(for JavaScript).
Let's walk through an example with num1 = "456"
and num2 = "77"
.
num1
and num2
. We process each digit once from right to left.The "Add Strings" problem is a classic example of simulating arithmetic operations on large numbers using string manipulation. By processing the digits from right to left and handling the carry at each step, we can add numbers of arbitrary length without worrying about integer overflow. The solution is efficient, easy to understand, and closely mimics manual addition, making it both practical and elegant.