MCPcopy Create free account
hub / github.com/StudyRust/leetcode_rust / reverse_str

Function reverse_str

541-reverse-string-ii.rs:2–16  ·  view source on GitHub ↗

good,练习chunk的使用。

(s: String, k: i32)

Source from the content-addressed store, hash-verified

1// good,练习chunk的使用。
2pub fn reverse_str(s: String, k: i32) -> String {
3 let mut ret_c = s.chars().collect::<Vec<char>>();
4 let mut idx = 0;
5 for chunk in ret_c.chunks_mut(k as usize) {
6 if idx % 2 == 0 {
7 let mut tmp = chunk.to_vec();
8 tmp.reverse();
9 for (i, elem) in chunk.iter_mut().enumerate() {
10 *elem = tmp[i];
11 }
12 }
13 idx += 1;
14 }
15 ret_c.iter().collect()
16}
17
18fn main() {
19 let s = "abcd".to_string();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected