Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
4eda7708
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
10 个月 前同步成功
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cs Summary Reflection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4eda7708
编写于
8月 04, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add rust-leetcode #1367
上级
f94f3d1f
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
125 addition
and
1 deletion
+125
-1
rust-leetcode/README.md
rust-leetcode/README.md
+2
-1
rust-leetcode/src/leetcode_1367.rs
rust-leetcode/src/leetcode_1367.rs
+113
-0
rust-leetcode/src/main.rs
rust-leetcode/src/main.rs
+1
-0
rust-leetcode/src/pre_structs.rs
rust-leetcode/src/pre_structs.rs
+9
-0
未找到文件。
rust-leetcode/README.md
浏览文件 @
4eda7708
...
...
@@ -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
)
## 链表&栈&队列
...
...
rust-leetcode/src/leetcode_1367.rs
0 → 100644
浏览文件 @
4eda7708
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
);
}
}
rust-leetcode/src/main.rs
浏览文件 @
4eda7708
...
...
@@ -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
;
...
...
rust-leetcode/src/pre_structs.rs
浏览文件 @
4eda7708
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录