Skip to content

Commit 618c9bd

Browse files
committed
1556. Thousand Separator: AC
1 parent 058b251 commit 618c9bd

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/solution/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1172,3 +1172,4 @@ mod s1550_three_consecutive_odds;
11721172
mod s1551_minimum_operations_to_make_array_equal;
11731173
mod s1552_magnetic_force_between_two_balls;
11741174
mod s1553_minimum_number_of_days_to_eat_n_oranges;
1175+
mod s1556_thousand_separator;
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* [1556] Thousand Separator
3+
*
4+
* Given an integer n, add a dot (".") as the thousands separator and return it in string format.
5+
*
6+
* Example 1:
7+
*
8+
* Input: n = 987
9+
* Output: "987"
10+
*
11+
* Example 2:
12+
*
13+
* Input: n = 1234
14+
* Output: "1.234"
15+
*
16+
*
17+
* Constraints:
18+
*
19+
* 0 <= n <= 2^31 - 1
20+
*
21+
*/
22+
pub struct Solution {}
23+
24+
// problem: https://leetcode.com/problems/thousand-separator/
25+
// discuss: https://leetcode.com/problems/thousand-separator/discuss/?currentPage=1&orderBy=most_votes&query=
26+
27+
// submission codes start here
28+
29+
impl Solution {
30+
pub fn thousand_separator(n: i32) -> String {
31+
n.to_string()
32+
.chars()
33+
.collect::<Vec<_>>()
34+
.rchunks(3)
35+
.rev()
36+
.map(|v| v.iter().collect::<String>())
37+
.collect::<Vec<_>>()
38+
.join(".")
39+
}
40+
}
41+
42+
// submission codes end
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
48+
#[test]
49+
fn test_1556_example_1() {
50+
let n = 987;
51+
52+
let result = "987".to_string();
53+
54+
assert_eq!(Solution::thousand_separator(n), result);
55+
}
56+
57+
#[test]
58+
fn test_1556_example_2() {
59+
let n = 1234;
60+
61+
let result = "1.234".to_string();
62+
63+
assert_eq!(Solution::thousand_separator(n), result);
64+
}
65+
}

0 commit comments

Comments
 (0)