Add Strings
Easy · strings
Two non-negative whole numbers are written as strings of decimal digits, num1 and num2. They can be far too long to fit in a normal integer type.
Return their sum as a string of digits, written the same way: no leading zeros, except that the number zero itself is written as "0".
Do the addition yourself, digit by digit. Do not convert a whole input string to a number and do not use a big-integer library.
Examples
Example 1
Input: num1 = "11", num2 = "123"
Output: "134"
The inputs have different lengths. Lining them up on the right gives 011 + 123 = 134.
Example 2
Input: num1 = "456", num2 = "77"
Output: "533"
6 + 7 = 13 writes 3 and carries 1, then 5 + 7 + 1 = 13 writes 3 and carries 1, then 4 + 1 = 5.
Example 3
Input: num1 = "999", num2 = "1"
Output: "1000"
The carry runs through every digit and finally adds a new leading digit.
Constraints
- 1 <= len(num1), len(num2) <= 10^4
- num1 and num2 contain only the digits 0 to 9.
- Neither input has a leading zero, except the number zero itself.