Given an integer n, add a dot (".") as the thousands separator and return it in string format.
Example 1:
Input: n = 987
Output: "987"
Example 2:
Input: n = 1234
Output: "1.234"
Constraints:
0 <= n <= 231 - 1
Solutions
Solution 1
Thinking
Insert a dot every three digits from the right. Converting to a string first requires extra care when the length is a multiple of three; peeling remainders from the low end is simpler.
Repeatedly take \(n\bmod 10\) and count digits; after every third digit, if a higher place remains, append a dot. Reverse the collected characters at the end. The loop runs once per digit, \(O(\log n)\).