1408.数组中的字符串匹配、623 改为自身递归

This commit is contained in:
游由 2022-08-06 17:43:56 +08:00
parent d448d45f07
commit a20d9cd7e4
3 changed files with 85 additions and 2 deletions

View File

@ -178,6 +178,7 @@ mod q1269;
mod q1310;
mod q1374;
mod q1403;
mod q1408;
mod q1447;
mod q1473;
mod q1482;

View File

@ -40,7 +40,7 @@ impl Solution {
/// * 👍 167
/// * 👎 0
pub fn add_one_row(root: Option<Rc<RefCell<TreeNode>>>, val: i32, depth: i32) -> Option<Rc<RefCell<TreeNode>>> {
fn add_one_row_recursion(mut node: Option<Rc<RefCell<TreeNode>>>, curr_depth: i32, val: i32, depth: i32) -> Option<Rc<RefCell<TreeNode>>> {
/*fn add_one_row_recursion(mut node: Option<Rc<RefCell<TreeNode>>>, curr_depth: i32, val: i32, depth: i32) -> Option<Rc<RefCell<TreeNode>>> {
match curr_depth.cmp(&depth) {
Ordering::Less => match node {
Some(mut n) => match n.borrow_mut() {
@ -77,7 +77,31 @@ impl Solution {
})))
}
}
add_one_row_recursion(root, 1, val, depth - 1)
add_one_row_recursion(root, 1, val, depth - 1)*/
match depth {
1 => Some(Rc::new(RefCell::new(TreeNode { val, left: root, right: None }))),
2 => match root {
Some(mut n) => match n.borrow_mut() {
mut p => Some(Rc::new(RefCell::new(TreeNode {
val: p.val,
left: Some(Rc::new(RefCell::new(TreeNode { val, left: p.left.take(), right: None }))),
right: Some(Rc::new(RefCell::new(TreeNode { val, left: None, right: p.right.take() }))),
})))
},
_ => None
}
_ => match root {
Some(mut n) => match n.borrow_mut() {
mut p => Some(Rc::new(RefCell::new(TreeNode {
val: p.val,
left: self::Solution::add_one_row(p.left.take(), val, depth - 1),
right: self::Solution::add_one_row(p.right.take(), val, depth - 1),
})))
},
_ => None
}
}
//unimplemented!()
}
}

View File

@ -0,0 +1,58 @@
use crate::Solution;
impl Solution {
/// [1408.数组中的字符串匹配](https://leetcode-cn.com/problems/string-matching-in-an-array/)
///
/// 2022-08-06 11:27:43
///
/// 给你一个字符串数组 `words` ,数组中的每个字符串都可以看作是一个单词。请你按 **任意** 顺序返回 `words` 中是其他单词的子字符串的所有单词。
///
/// 如果你可以删除 `words[j]` 最左侧和/或最右侧的若干字符得到 `word[i]` ,那么字符串 `words[i]` 就是 `words[j]` 的一个子字符串。
///
/// + **示例 1**
/// + **输入:** words = \["mass","as","hero","superhero"\]
/// + **输出:** \["as","hero"\]
/// + **解释:**
/// + "as" 是 "mass" 的子字符串,"hero" 是 "superhero" 的子字符串。
/// + \["hero","as"\] 也是有效的答案。
/// + **示例 2**
/// + **输入:**words = \["leetcode","et","code"\]
/// + **输出:**\["et","code"\]
/// + **解释:**"et" 和 "code" 都是 "leetcode" 的子字符串。
/// + **示例 3**
/// + **输入:** words = \["blue","green","bu"\]
/// + **输出:** \[\]
/// + **提示:**
/// * `1 <= words.length <= 100`
/// * `1 <= words[i].length <= 30`
/// * `words[i]` 仅包含小写英文字母。
/// * 题目数据 **保证** 每个 `words[i]` 都是独一无二的。
/// + Related Topics
/// * 字符串
/// * 字符串匹配
/// * 👍 55
/// * 👎 0
pub fn string_matching(mut words: Vec<String>) -> Vec<String> {
words.sort_unstable_by_key(|a| a.len());
let mut result = vec![];
for i in 0..words.len() {
for j in i + 1..words.len() {
if words[j].contains(words[i].as_str()) {
result.push(words[i].clone());
break;
}
}
}
result
}
}
#[cfg(test)]
mod test {
use crate::Solution;
#[test]
fn test_q1408() {
assert_eq!(Solution::string_matching(vec!["mass".to_owned(), "as".to_owned(), "hero".to_owned(), "superhero".to_owned()]), vec!["as".to_owned(), "hero".to_owned()]);
}
}