Skip to content

Commit 7fae40a

Browse files
authored
Create thousand-separator.cpp
1 parent a47718f commit 7fae40a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

C++/thousand-separator.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
string thousandSeparator(int n) {
7+
string result;
8+
const auto& s = to_string(n);
9+
for (int i = 0; i < s.length(); ++i) {
10+
if (i > 0 && (s.length() - i) % 3 == 0) {
11+
result += ".";
12+
}
13+
result += s[i];
14+
}
15+
return result;
16+
}
17+
};

0 commit comments

Comments
 (0)