Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
fbf6d661
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
fbf6d661
编写于
3月 18, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor code
上级
c6e91bbe
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
247 addition
and
1 deletion
+247
-1
rust-leetcode/README.md
rust-leetcode/README.md
+210
-1
rust-leetcode/src/Solution.rs
rust-leetcode/src/Solution.rs
+37
-0
未找到文件。
rust-leetcode/README.md
浏览文件 @
fbf6d661
Leetcode
超简单的算法题目,主要为了熟悉rust语法
Leetcode
Rust 实现
--
超简单的算法题目,主要为了熟悉rust语法。源码在Solution.rs,并包含测试
*
返回倒数第 k 个节点值
```
rust
impl
Solution
{
pub
fn
kth_to_last
(
head
:
Option
<
Box
<
ListNode
>>
,
k
:
i32
)
->
i32
{
let
mut
nodes
=
head
;
let
mut
qs
=
VecDeque
::
new
();
loop
{
if
let
Some
(
node
)
=
nodes
.borrow
()
{
qs
.push_back
(
node
.val
);
nodes
=
node
.next
.clone
();
}
else
{
break
;
}
}
let
i
=
qs
.len
()
-
k
as
usize
;
let
ret
=
qs
.get
(
i
);
*
ret
.unwrap
()
}
//倒数第k个,位置就是len-k。即快指针先走k步,然后2个指针同时走,快指针到达尾时,慢指针的位置就是第len-k个元素。此时快指针刚好走完一圈
pub
fn
kth_to_last2
(
head
:
Option
<
Box
<
ListNode
>>
,
k
:
i32
)
->
i32
{
let
mut
i
=
k
;
let
mut
fast
=
head
.as_ref
();
//clone也可以,但没有必要,不能copy,没有实现Copy
let
mut
slow
=
head
.as_ref
();
while
i
>
0
{
if
let
Some
(
node
)
=
fast
.borrow
()
{
fast
=
node
.next
.as_ref
();
i
-=
1
;
}
else
{
break
;
}
}
while
fast
!=
None
{
if
let
Some
(
node
)
=
fast
.borrow
()
{
fast
=
node
.next
.as_ref
();
if
let
Some
(
node
)
=
slow
.borrow
()
{
slow
=
node
.next
.as_ref
();
}
}
}
slow
.unwrap
()
.val
}
}
```
*
链表中倒数第k个节点
```
rust
impl
Solution
{
pub
fn
get_kth_from_end
(
head
:
Option
<
Box
<
ListNode
>>
,
k
:
i32
)
->
Option
<
Box
<
ListNode
>>
{
let
mut
i
=
k
;
let
mut
fast
=
head
.as_ref
();
let
mut
slow
=
head
.as_ref
();
while
i
>
0
{
if
let
Some
(
node
)
=
fast
.borrow
()
{
fast
=
node
.next
.as_ref
();
i
-=
1
;
}
}
while
fast
!=
None
{
if
let
Some
(
node
)
=
fast
.borrow
()
{
fast
=
node
.next
.as_ref
();
if
let
Some
(
node
)
=
slow
.borrow
()
{
slow
=
node
.next
.as_ref
();
}
}
}
Some
(
slow
.unwrap
()
.clone
())
}
}
```
*
统计有序矩阵中的负数
```
rust
impl
Solution
{
//应该将矩阵是排序的考虑进去,从右下角或左下角使用标记
pub
fn
count_negatives
(
grid
:
Vec
<
Vec
<
i32
>>
)
->
i32
{
let
mut
count
:
i32
=
0
;
for
r
in
grid
.iter
()
{
count
+=
r
.iter
()
.filter
(|
&&
x
|
x
<
0
)
.count
()
as
i32
}
return
count
;
}
}
```
*
二叉树的深度
```
rust
impl
Solution
{
pub
fn
max_depth
(
root
:
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
i32
{
fn
get_depth
(
root
:
&
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
i32
{
if
let
Some
(
root
)
=
root
{
let
node
=
root
.try_borrow
()
.unwrap
();
return
max
(
get_depth
(
&
node
.left
),
get_depth
(
&
node
.right
))
+
1
;
}
else
{
return
0
;
}
}
get_depth
(
&
root
)
}
}
```
*
最小高度树
```
rust
impl
Solution
{
pub
fn
sorted_array_to_bst
(
nums
:
Vec
<
i32
>
)
->
Option
<
Rc
<
RefCell
<
TreeNode
>>>
{
fn
buildTree
(
nums
:
&
Vec
<
i32
>
,
l
:
i32
,
r
:
i32
)
->
Option
<
Rc
<
RefCell
<
TreeNode
>>>
{
if
l
>
r
{
return
None
;
}
if
l
==
r
{
return
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
::
new
(
nums
[
l
as
usize
]))));
}
let
mid
=
l
+
(
r
-
l
)
/
2
;
let
mut
root
=
TreeNode
::
new
(
nums
[
mid
as
usize
]);
root
.left
=
buildTree
(
nums
,
l
,
mid
-
1
);
root
.right
=
buildTree
(
nums
,
mid
+
1
,
r
);
return
Some
(
Rc
::
new
(
RefCell
::
new
(
root
)));
}
return
buildTree
(
&
nums
,
0
,
(
nums
.len
()
-
1
)
as
i32
);
}
}
```
*
整数的各位积和之差
```
rust
impl
Solution
{
pub
fn
subtract_product_and_sum
(
n
:
i32
)
->
i32
{
let
mut
num
=
n
;
let
mut
muti
=
1
;
let
mut
sum
=
0
;
while
num
!=
0
{
let
mut
tmp
=
num
%
10
;
muti
*=
tmp
;
sum
+=
tmp
;
num
/=
10
;
}
muti
-
sum
}
}
```
*
左旋转字符串
```
rust
impl
Solution
{
pub
fn
reverse_left_words
(
s
:
String
,
n
:
i32
)
->
String
{
let
mut
s1
=
String
::
from
(
&
s
[
0
..
n
as
usize
]);
let
s2
=
&
s
[
n
as
usize
..
s
.len
()];
s1
.insert_str
(
0
,
s2
);
s1
.to_owned
()
}
}
```
*
有多少小于当前数字的数字
```
rust
impl
Solution
{
pub
fn
smaller_numbers_than_current
(
nums
:
Vec
<
i32
>
)
->
Vec
<
i32
>
{
let
mut
ret
=
Vec
::
with_capacity
(
nums
.len
());
for
i
in
0
..
nums
.len
()
{
let
mut
count
=
0
;
for
j
in
0
..
nums
.len
()
{
if
nums
[
i
]
>
nums
[
j
]
{
count
+=
1
;
}
}
ret
.push
(
count
);
}
ret
}
}
```
*
将数字变成 0 的操作次数
```
rust
impl
Solution
{
pub
fn
number_of_steps
(
num
:
i32
)
->
i32
{
let
mut
n
=
num
;
let
mut
i
=
0
;
while
n
!=
0
{
if
n
&
1
==
0
{
n
/=
2
;
}
else
{
n
-=
1
;
}
i
+=
1
;
};
i
}
}
```
*
解压缩编码列表
```
rust
impl
Solution
{
pub
fn
decompress_rl_elist
(
nums
:
Vec
<
i32
>
)
->
Vec
<
i32
>
{
let
mut
rets
=
Vec
::
new
();
for
(
index
,
e
)
in
nums
.iter
()
.enumerate
()
{
if
index
&
1
==
0
{
let
mut
freq
=
nums
[
index
];
let
value
=
nums
[
index
+
1
];
while
freq
!=
0
{
rets
.push
(
value
);
freq
-=
1
;
}
}
}
rets
}
}
```
\ No newline at end of file
rust-leetcode/src/Solution.rs
浏览文件 @
fbf6d661
...
...
@@ -63,6 +63,7 @@ pub fn solutions() {
interview_55_1
();
leetcode_1351
();
interview_02_02
();
interview_22
();
}
///返回倒数第 k 个节点
...
...
@@ -123,6 +124,42 @@ fn interview_02_02() {
println!
(
"{}"
,
ret2
);
}
///链表中倒数第k个节点
fn
interview_22
()
{
println!
(
"interview_22"
);
impl
Solution
{
pub
fn
get_kth_from_end
(
head
:
Option
<
Box
<
ListNode
>>
,
k
:
i32
)
->
Option
<
Box
<
ListNode
>>
{
let
mut
i
=
k
;
let
mut
fast
=
head
.as_ref
();
let
mut
slow
=
head
.as_ref
();
while
i
>
0
{
if
let
Some
(
node
)
=
fast
.borrow
()
{
fast
=
node
.next
.as_ref
();
i
-=
1
;
}
}
while
fast
!=
None
{
if
let
Some
(
node
)
=
fast
.borrow
()
{
fast
=
node
.next
.as_ref
();
if
let
Some
(
node
)
=
slow
.borrow
()
{
slow
=
node
.next
.as_ref
();
}
}
}
Some
(
slow
.unwrap
()
.clone
())
}
}
let
e1
=
Some
(
Box
::
new
(
ListNode
{
val
:
5
,
next
:
None
}));
let
e2
=
Some
(
Box
::
new
(
ListNode
{
val
:
4
,
next
:
e1
}));
let
e3
=
Some
(
Box
::
new
(
ListNode
{
val
:
3
,
next
:
e2
}));
let
e4
=
Some
(
Box
::
new
(
ListNode
{
val
:
2
,
next
:
e3
}));
let
e5
=
Some
(
Box
::
new
(
ListNode
{
val
:
1
,
next
:
e4
}));
let
ret
=
Solution
::
get_kth_from_end
(
e5
,
2
);
println!
(
"{:?}"
,
ret
);
}
///统计有序矩阵中的负数
fn
leetcode_1351
()
{
println!
(
"leetcode_1351"
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录