← Home

415. Add Strings

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
415

A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 415. Add Strings, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are addStrings.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 8 ms, faster than 96.87% of C++ online submissions for Add Strings.
02//Memory Usage: 8.8 MB, less than 100.00% of C++ online submissions for Add Strings.
03
04class Solution {
05public:
06    string addStrings(string num1, string num2) {
07        int N = max(num1.size(), num2.size());
08        string ans;
09        int carry = 0;
10        int d1, d2;
11        
12        reverse(num1.begin(), num1.end());
13        reverse(num2.begin(), num2.end());
14        
15        for(int i = 0; i < N; i++){
16            if(i < num1.size())d1 = num1[i]-'0';
17            else d1 = 0;
18            if(i < num2.size())d2 = num2[i]-'0';
19            else d2 = 0;
20            ans += ((d1+d2+carry)%10 + '0');
21            carry = (d1+d2+carry)/10;
22        }
23        if(carry > 0 )ans += (carry + '0');
24        reverse(ans.begin(), ans.end());
25        return ans;
26    }
27};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.