← Home

1556. Thousand Separator

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

I like to read this solution as a small machine: keep the useful information, throw away the noise. For 1556. Thousand Separator, 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 thousandSeparator.

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. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

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 Thousand Separator.
02//Memory Usage: 6.1 MB, less than 20.00% of C++ online submissions for Thousand Separator.
03class Solution {
04public:
05    string thousandSeparator(int n) {
06        if(n == 0) return "0";
07        
08        string ans = "";
09        
10        while(n){
11            string tmp = to_string(n%1000);
12            /*
13            need to pad 0 for the case:
14            51040
15            */
16            if(tmp.size() < 3 && n >= 1000){
17                tmp = string(3-tmp.size(), '0') + tmp;
18            }
19            if(ans.empty()){
20                ans = tmp;
21            }else{
22                ans = tmp + "." + ans;
23            }
24            n /= 1000;
25            // cout << ans << endl;
26        }
27        
28        return ans;
29    }
30};
31
32//https://leetcode.com/problems/thousand-separator/discuss/805704/C%2B%2B-Iterative
33//Runtime: 4 ms, faster than 20.00% of C++ online submissions for Thousand Separator.
34//Memory Usage: 5.9 MB, less than 60.00% of C++ online submissions for Thousand Separator.
35class Solution {
36public:
37    string thousandSeparator(int n) {
38        string s = to_string(n);
39        string res = "";
40        
41        for(int i = 0; i < s.size(); ++i){
42            /*
43            there are "s.size()-i" chars in s[i:],
44            we add "." before the digit such that 
45            there are 3's multiple digits behind it(including)
46            */
47            if(i > 0 && (s.size()-i)%3 == 0){
48                res += ".";
49            }
50            res += s[i];
51        }
52        
53        return res;
54    }
55};

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.