Add Strings
EasyString · NVIDIA
You are given two non-negative integers, num1 and num2, each written as a string of decimal digits. Return their sum, also as a string of decimal digits.
Do the addition yourself, one digit at a time, the way you would on paper. Converting a whole input to a built-in integer (for example int(num1)) or using a big-number library is not allowed.
- Neither input has leading zeros, except the number
0itself, which is written as"0". - The returned string must not have leading zeros either.
Example 1
- Input:
- num1 = "478", num2 = "56"
- Output:
- "534"
- Explanation
- Line the numbers up on the right. 8 + 6 = 14 (write 4, carry 1), 7 + 5 + 1 = 13 (write 3, carry 1), then 4 + 1 = 5. Reading the written digits gives
"534".
Example 2
- Input:
- num1 = "9995", num2 = "7"
- Output:
- "10002"
- Explanation
- 5 + 7 = 12 leaves a carry that ripples through all three 9s, so the answer gains a new leading digit.
Example 3
- Input:
- num1 = "0", num2 = "250"
- Output:
- "250"
- Explanation
- Adding zero leaves the other number unchanged; the shorter input simply contributes 0 in the missing columns.
Constraints
1 <= num1.length, num2.length <= 10^4num1andnum2contain only the digits0-9- Neither input has leading zeros, except
"0"itself