From ed3b4dbebc64e3f1a6a0f93ff3073dce7160c85f Mon Sep 17 00:00:00 2001 From: jtr109 Date: Wed, 19 Aug 2020 14:38:22 +0800 Subject: [PATCH 1/2] add test --- Cargo.lock | 4 ++++ Cargo.toml | 1 + README.md | 1 + reverse_words_in_a_string_iii/Cargo.toml | 9 ++++++++ reverse_words_in_a_string_iii/src/lib.rs | 29 ++++++++++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 reverse_words_in_a_string_iii/Cargo.toml create mode 100644 reverse_words_in_a_string_iii/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 2b70ee4..4f75e37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,6 +28,10 @@ version = "0.1.0" name = "reverse_integer" version = "0.1.0" +[[package]] +name = "reverse_words_in_a_string_iii" +version = "0.1.0" + [[package]] name = "three_sum" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 5b44961..9eb1a88 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ members = [ "merge_sorted_array", "intersection_of_two_arrays", "longest_palindromic_substring", + "reverse_words_in_a_string_iii", ] diff --git a/README.md b/README.md index 9c27bd7..d0e4792 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Resolving problems of LeetCode in RustLang. * [215. Kth Largest Element in an Array](./kth_largest/src/lib.rs) * [232. Implement Queue using Stacks](./implement_queue_using_stacks/src/lib.rs) * [349. Intersection of Two Arrays](./intersection_of_two_arrays/src/lib.rs) +* [557. Reverse Words in a String III](./reverse_words_in_a_string_iii/src/lib.rs) ## Document diff --git a/reverse_words_in_a_string_iii/Cargo.toml b/reverse_words_in_a_string_iii/Cargo.toml new file mode 100644 index 0000000..1ae19c4 --- /dev/null +++ b/reverse_words_in_a_string_iii/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "reverse_words_in_a_string_iii" +version = "0.1.0" +authors = ["Ryan Li "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/reverse_words_in_a_string_iii/src/lib.rs b/reverse_words_in_a_string_iii/src/lib.rs new file mode 100644 index 0000000..a9e05fb --- /dev/null +++ b/reverse_words_in_a_string_iii/src/lib.rs @@ -0,0 +1,29 @@ +/*! + * # 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 { + "".to_string() + } +} + +#[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); + } +} From 4adfc6fa318324d69f826ef12e673ce5afdf6f0f Mon Sep 17 00:00:00 2001 From: jtr109 Date: Wed, 19 Aug 2020 14:53:11 +0800 Subject: [PATCH 2/2] add solution --- reverse_words_in_a_string_iii/src/lib.rs | 26 +++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/reverse_words_in_a_string_iii/src/lib.rs b/reverse_words_in_a_string_iii/src/lib.rs index a9e05fb..58b5e23 100644 --- a/reverse_words_in_a_string_iii/src/lib.rs +++ b/reverse_words_in_a_string_iii/src/lib.rs @@ -12,7 +12,31 @@ struct Solution {} impl Solution { pub fn reverse_words(s: String) -> String { - "".to_string() + let mut i = 0; + let chars = s.chars().collect::>(); + 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() } }