bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 415. Add Strings

415. Add Strings

Leetcode Solutions

Elementary Math - Digit-by-Digit Addition

  1. Initialize an empty result string res.
  2. Initialize carry to 0.
  3. Set pointers p1 and p2 at the end of num1 and num2 respectively.
  4. While p1 >= 0 or p2 >= 0: a. Get the digit x1 from num1 at p1 or 0 if p1 < 0. b. Get the digit x2 from num2 at p2 or 0 if p2 < 0. c. Calculate the sum value = (x1 + x2 + carry) % 10. d. Update carry = (x1 + x2 + carry) / 10. e. Append value to res. f. Decrement p1 and p2.
  5. If carry is non-zero, append it to res.
  6. Reverse res.
  7. Return res as a string.
UML Thumbnail

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...