← Home

1317. Convert Integer to the Sum of Two No-Zero Integers

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

The trick here is to name the state correctly, then let the implementation follow. For 1317. Convert Integer to the Sum of Two No-Zero Integers, 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.

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 getNoZeroIntegers.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • 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: 0 ms, faster than 100.00% of C++ online submissions for Convert Integer to the Sum of Two No-Zero Integers.
02//Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Convert Integer to the Sum of Two No-Zero Integers.
03
04class Solution {
05public:
06    vector<int> getNoZeroIntegers(int n) {
07        for(int i = 1; i < n; i++){
08            int j = n - i;
09            string si = to_string(i), sj = to_string(j);
10            // cout << si << ", " << sj << endl;
11            
12            if(!count(si.begin(), si.end(), '0') &&
13               !count(sj.begin(), sj.end(), '0')){
14                return {i, j};
15            }
16        }
17        return {0,0};
18    }
19};

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.