#include<string>usingnamespace std;
classSolution {
public: string thousandSeparator(int n) {
string s = to_string(n), res;
int cnt =0;
for (int i = s.size()-1; i >=0; --i) {
if (cnt && cnt %3==0) res ='.'+ res;
res = s[i] + res;
++cnt;
}
return res;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
public String thousandSeparator(int n) {
String s = Integer.toString(n);
StringBuilder sb =new StringBuilder();
int cnt = 0;
for (int i = s.length() - 1; i >= 0; --i) {
if (cnt > 0 && cnt % 3 == 0) sb.append('.');
sb.append(s.charAt(i));
cnt++;
}
return sb.reverse().toString();
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defthousandSeparator(self, n: int) -> str:
s = str(n)
res = []
for i, c in enumerate(reversed(s)):
if i and i %3==0:
res.append('.')
res.append(c)
return''.join(reversed(res))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
impl Solution {
pubfnthousand_separator(n: i32) -> String {
let s = n.to_string();
letmut res = String::new();
letmut cnt =0;
for c in s.chars().rev() {
if cnt >0&& cnt %3==0 {
res.push('.');
}
res.push(c);
cnt +=1;
}
res.chars().rev().collect()
}
}
1
2
3
4
5
6
7
8
9
10
11
functionthousandSeparator(n: number):string {
consts=n.toString();
letres='';
letcnt=0;
for (leti=s.length-1; i>=0; --i) {
if (cnt>0&&cnt%3===0) res='.'+res;
res=s[i] +res;
cnt++;
}
returnres;
}