From 4eda7708a9a2a9c43063d606cd1076e5c4d24b76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=A6=E5=A2=83=E8=BF=B7=E7=A6=BB?= Date: Tue, 4 Aug 2020 20:09:59 +0800 Subject: [PATCH] add rust-leetcode #1367 --- rust-leetcode/README.md | 3 +- rust-leetcode/src/leetcode_1367.rs | 113 +++++++++++++++++++++++++++++ rust-leetcode/src/main.rs | 1 + rust-leetcode/src/pre_structs.rs | 9 +++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 rust-leetcode/src/leetcode_1367.rs diff --git a/rust-leetcode/README.md b/rust-leetcode/README.md index 8a20a61e..2ccad238 100644 --- a/rust-leetcode/README.md +++ b/rust-leetcode/README.md @@ -3,7 +3,7 @@ Leetcode Rust 实现 超简单的算法题目,主要为了熟悉rust语法。源码在old_solutions.rs(未使用单测的)或leetcode-*.rs(使用了rust单测的),70%~90%是双100。 -# author: dreamylost +若有其他人想贡献Rust的LeetCode,请参照现有格式新增文件(同时将文件导入到`main.rs`)。如果题目已经存在,可以在`leetcode_xx.rs`中添加第二个方法实现。 ## 树 @@ -24,6 +24,7 @@ Leetcode Rust 实现 * [617 合并二叉树](src/leetcode_617.rs) * [100 相同的树](src/leetcode_100.rs) +* [1367 二叉树中的列表](src/leetcode_1367.rs) ## 链表&栈&队列 diff --git a/rust-leetcode/src/leetcode_1367.rs b/rust-leetcode/src/leetcode_1367.rs new file mode 100644 index 00000000..a102a0f2 --- /dev/null +++ b/rust-leetcode/src/leetcode_1367.rs @@ -0,0 +1,113 @@ +use std::borrow::{Borrow, BorrowMut}; +use std::cell::RefCell; +use std::rc::Rc; + +use crate::pre_structs::{print_vec, ListNode, Solution, TreeNode}; + +///1367. 二叉树中的列表 +impl Solution { + //暴力干 + //20 ms,50.00% + //3.8 MB,100.00% + pub fn is_sub_path(head: Option>, root: Option>>) -> bool { + fn dfs_get_paths( + root: &Option>>, + mut path: String, + ret: &mut Vec, + ) { + if let Some(root) = root { + if root.as_ref().borrow().left.is_none() && root.as_ref().borrow().right.is_none() { + path += &root.as_ref().borrow().val.to_string(); + ret.push(path.to_string()); + return; + } + path += &root.as_ref().borrow().val.to_string(); + path += &"".to_string(); + dfs_get_paths(&root.as_ref().borrow().left, path.clone(), ret); + dfs_get_paths(&root.as_ref().borrow().right, path, ret); + } else { + return; + } + } + let mut p = "".to_owned(); + let mut ps = Vec::new(); + dfs_get_paths(&root, p, &mut ps); + let mut target: String = String::new(); + let mut h = head.as_ref(); + while h.borrow().is_some() { + target.push_str(&h.borrow().unwrap().val.to_string()); + h = h.borrow().unwrap().next.as_ref(); + } + + for list in ps.iter() { + if list.contains(&target) { + return true; + } + } + + false + } + + //使用类似处理is_sub的方法 + //52 ms,50.00% + //7.3 MB,100.00% + pub fn is_sub_path2(head: Option>, root: Option>>) -> bool { + fn is_sub(head: &Option>, root: &Option>>) -> bool { + if head.is_none() { + return true; + } + if root.is_none() { + return false; + } + unsafe { + if head.borrow().as_ref().unwrap().val + != (*root.borrow().as_ref().unwrap().as_ptr()).val + { + return false; + } + return is_sub( + &head.borrow().as_ref().unwrap().next, + &(*root.borrow().as_ref().unwrap().as_ptr()).left, + ) || is_sub( + &head.borrow().as_ref().unwrap().next, + &(*root.borrow().as_ref().unwrap().as_ptr()).right, + ); + } + } + let root = &root; + if head.is_none() { + return true; + } + if root.is_none() { + return false; + } + unsafe { + is_sub(&head, root) + || Solution::is_sub_path2( + head.clone(), + (*root.borrow().as_ref().unwrap().as_ptr()).right.clone(), + ) + || Solution::is_sub_path2( + head, + (*root.borrow().as_ref().unwrap().as_ptr()).left.clone(), + ) + } + } +} + +#[cfg(test)] +mod test { + use crate::pre_structs::{get_test_list_1, get_test_tree_5, Solution}; + + #[test] + fn is_sub_path() { + let ret = Solution::is_sub_path(get_test_list_1(), get_test_tree_5()); + println!("{}", ret); + } + + #[test] + fn is_sub_path2() { + let ret = Solution::is_sub_path2(get_test_list_1(), get_test_tree_5()); + println!("{}", ret); + } +} diff --git a/rust-leetcode/src/main.rs b/rust-leetcode/src/main.rs index 46607f88..f02fc98f 100644 --- a/rust-leetcode/src/main.rs +++ b/rust-leetcode/src/main.rs @@ -40,6 +40,7 @@ pub mod leetcode_1323; pub mod leetcode_1342; pub mod leetcode_1351; pub mod leetcode_1365; +pub mod leetcode_1367; pub mod leetcode_1370; pub mod leetcode_1374; pub mod leetcode_1380; diff --git a/rust-leetcode/src/pre_structs.rs b/rust-leetcode/src/pre_structs.rs index 8587fc48..cf1c1083 100644 --- a/rust-leetcode/src/pre_structs.rs +++ b/rust-leetcode/src/pre_structs.rs @@ -37,6 +37,15 @@ impl ListNode { } } +pub fn get_test_list_1() -> Option> { + let mut node2 = Some(Box::new(ListNode::new(9))); + let mut node1 = Some(Box::new(ListNode { + val: 3, + next: node2, + })); + node1 +} + pub fn get_test_tree_5() -> Option>> { let e7 = Some(Rc::new(RefCell::new(TreeNode { val: 7, -- GitLab