提交 4eda7708 编写于 作者: 梦境迷离's avatar 梦境迷离

add rust-leetcode #1367

上级 f94f3d1f
......@@ -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)
## 链表&栈&队列
......
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<Box<ListNode>>, root: Option<Rc<RefCell<TreeNode>>>) -> bool {
fn dfs_get_paths(
root: &Option<Rc<RefCell<TreeNode>>>,
mut path: String,
ret: &mut Vec<String>,
) {
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<Box<ListNode>>, root: Option<Rc<RefCell<TreeNode>>>) -> bool {
fn is_sub(head: &Option<Box<ListNode>>, root: &Option<Rc<RefCell<TreeNode>>>) -> 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);
}
}
......@@ -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;
......
......@@ -37,6 +37,15 @@ impl ListNode {
}
}
pub fn get_test_list_1() -> Option<Box<ListNode>> {
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<Rc<RefCell<TreeNode>>> {
let e7 = Some(Rc::new(RefCell::new(TreeNode {
val: 7,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册