← Home

258. Add Digits

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

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 258. Add Digits, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. 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.

The notes already sitting in the source point us in the right direction:

  • Congruence formula

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are addDigits.

Guide

Why?

The code is doing bookkeeping so your brain does not have to keep the entire search space open at once.

  • 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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

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(1), space: O(1)
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Congruence formula
02/*
03num = d0*1 + d1*10 + d2*100 + ... + dk*10^k
04= d0*1 + d1*(1+9) + d2*(1+99) + ... dk*(1+9..9)
05= d0 + d1 + ... dk + (d1*9 + d2*99 + dk*9..9)
06
07num % 9 = (d0+...dk) % 9
08*/
09//Runtime: 0 ms, faster than 100.00% of C++ online submissions for Add Digits.
10//Memory Usage: 5.8 MB, less than 97.98% of C++ online submissions for Add Digits.
11//time: O(1), space: O(1)
12class Solution {
13public:
14    int addDigits(int num) {
15        if(num == 0) return 0;
16        if(num % 9 == 0) return 9;
17        return num % 9;
18    }
19};
20
21//combine the last two cases
22class Solution {
23public:
24    int addDigits(int num) {
25        if(num == 0) return 0;
26        return (num-1)%9+1;
27    }
28};

Cost

Complexity

Time
O(1), space: O(1)
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.