-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
53 lines (47 loc) · 1.32 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*!
* # 557. Reverse Words in a String III
*
* [Problem link](https://leetcode.com/problems/reverse-words-in-a-string-iii/)
*/
#![allow(dead_code)]
struct Solution {}
// ----------------------------------------------------------------------------
impl Solution {
pub fn reverse_words(s: String) -> String {
let mut i = 0;
let chars = s.chars().collect::<Vec<char>>();
let mut reversed = vec![];
while i < s.len() {
if chars[i] == ' ' {
reversed.push(chars[i]);
i += 1;
continue;
}
let mut j = i;
while j < s.len() && chars[j] != ' ' {
j += 1;
}
let mut k = j - 1;
loop {
// push the char into the reversed vector until reach the index `i`
reversed.push(chars[k]);
if k == i {
break;
}
k -= 1;
}
i = j;
}
reversed.iter().collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_example_1() {
let input = "Let's take LeetCode contest".to_string();
let output = "s'teL ekat edoCteeL tsetnoc".to_string();
assert_eq!(Solution::reverse_words(input), output);
}
}