-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 按分隔符拆分字符串 | ||
// https://leetcode.cn/problems/split-strings-by-separator | ||
// INLINE ../../images/array/split_strings_by_separator.jpeg | ||
|
||
pub struct Solution; | ||
|
||
impl Solution { | ||
pub fn split_words_by_separator(words: Vec<String>, separator: char) -> Vec<String> { | ||
let mut res: Vec<String> = Vec::new(); | ||
for word in words { | ||
let ss = word.split(separator); | ||
for sub in ss { | ||
if !sub.is_empty() { | ||
res.push(sub.to_string()); | ||
} | ||
} | ||
} | ||
res | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use rust_practice::array::split_strings_by_separator::Solution; | ||
|
||
#[test] | ||
fn split_words_by_separator_test() { | ||
// 示例 1: | ||
// 输入:words = ["one.two.three","four.five","six"], separator = "." | ||
// 输出:["one","two","three","four","five","six"] | ||
// 解释:在本示例中,我们进行下述拆分: | ||
|
||
// "one.two.three" 拆分为 "one", "two", "three" | ||
// "four.five" 拆分为 "four", "five" | ||
// "six" 拆分为 "six" | ||
|
||
// 因此,结果数组为 ["one","two","three","four","five","six"] 。 | ||
let words = vec![ | ||
"one.two.three".to_string(), | ||
"four.five".to_string(), | ||
"six".to_string(), | ||
]; | ||
let separator = '.'; | ||
let result = Solution::split_words_by_separator(words, separator); | ||
assert_eq!( | ||
result, | ||
vec![ | ||
"one".to_string(), | ||
"two".to_string(), | ||
"three".to_string(), | ||
"four".to_string(), | ||
"five".to_string(), | ||
"six".to_string(), | ||
] | ||
); | ||
|
||
// 示例 2: | ||
// 输入:words = ["$easy$","$problem$"], separator = "$" | ||
// 输出:["easy","problem"] | ||
// 解释:在本示例中,我们进行下述拆分: | ||
|
||
// "$easy$" 拆分为 "easy"(不包括空字符串) | ||
// "$problem$" 拆分为 "problem"(不包括空字符串) | ||
|
||
// 因此,结果数组为 ["easy","problem"] 。 | ||
let words = vec!["$easy$".to_string(), "$problem$".to_string()]; | ||
let separator = '$'; | ||
let result = Solution::split_words_by_separator(words, separator); | ||
assert_eq!(result, vec!["easy".to_string(), "problem".to_string()]); | ||
|
||
// 示例 3: | ||
// 输入:words = ["|||"], separator = "|" | ||
// 输出:[] | ||
// 解释:在本示例中,"|||" 的拆分结果将只包含一些空字符串,所以我们返回一个空数组 [] 。 | ||
let words = vec!["|||".to_string()]; | ||
let separator = '|'; | ||
let result = Solution::split_words_by_separator(words, separator); | ||
assert_eq!(result.len(), 0); | ||
} |