Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
aed4a1f3
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 搜索 >>
提交
aed4a1f3
编写于
8月 11, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'origin/master'
上级
c061b7ba
403dee92
变更
15
隐藏空白更改
内联
并排
Showing
15 changed file
with
532 addition
and
14 deletion
+532
-14
README-en.md
README-en.md
+3
-3
README.md
README.md
+3
-3
java-leetcode/src/main/java/io/github/wkk/everyday/aug/RecoverBinarySearchTree.java
...a/io/github/wkk/everyday/aug/RecoverBinarySearchTree.java
+64
-0
java-leetcode/src/main/java/io/github/wkk/everyday/aug/RestoreIPAddresses.java
...n/java/io/github/wkk/everyday/aug/RestoreIPAddresses.java
+92
-0
java-leetcode/src/main/java/io/github/wkk/everyday/aug/SameTree.java
...de/src/main/java/io/github/wkk/everyday/aug/SameTree.java
+22
-0
java-leetcode/src/main/java/io/github/wkk/wkk.md
java-leetcode/src/main/java/io/github/wkk/wkk.md
+3
-1
rust-leetcode/README.md
rust-leetcode/README.md
+1
-0
rust-leetcode/src/interview_55_1.rs
rust-leetcode/src/interview_55_1.rs
+4
-2
rust-leetcode/src/leetcode_107.rs
rust-leetcode/src/leetcode_107.rs
+8
-5
rust-leetcode/src/leetcode_513.rs
rust-leetcode/src/leetcode_513.rs
+103
-0
rust-leetcode/src/leetcode_515.rs
rust-leetcode/src/leetcode_515.rs
+82
-0
rust-leetcode/src/main.rs
rust-leetcode/src/main.rs
+2
-0
scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_93.scala
...ode/src/main/scala/io/github/sweeneycai/Leetcode_93.scala
+64
-0
scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_99.scala
...ode/src/main/scala/io/github/sweeneycai/Leetcode_99.scala
+76
-0
scala-leetcode/src/main/scala/io/github/sweeneycai/sweeneycai.md
...eetcode/src/main/scala/io/github/sweeneycai/sweeneycai.md
+5
-0
未找到文件。
README-en.md
浏览文件 @
aed4a1f3
...
...
@@ -6,8 +6,8 @@
[
![Security Rating
](
https://sonarcloud.io/api/project_badges/measure?project=jxnu-liguobin_cs-summary-reflection&metric=security_rating
)
](https://sonarcloud.io/dashboard?id=jxnu-liguobin_cs-summary-reflection)
[
![
](
https://img.shields.io/github/languages/count/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
[
![
](
https://img.shields.io/github/languages/top/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
[
![
](
https://img.shields.io/lgtm/alerts/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
dreamylost.cn
)
[
![
](
https://img.shields.io/lgtm/grade/python/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
dreamylost.c
n)
[
![
](
https://img.shields.io/lgtm/alerts/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
lgtm.com/projects/g/jxnu-liguobin/cs-summary-reflection/alerts/?mode=list
)
[
![
](
https://img.shields.io/lgtm/grade/python/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
lgtm.com/projects/g/jxnu-liguobin/cs-summary-reflection/context:pytho
n)
[
![Language grade: Java
](
https://img.shields.io/lgtm/grade/java/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://lgtm.com/projects/g/jxnu-liguobin/cs-summary-reflection/context:java)
[
![
](
https://img.shields.io/github/commit-activity/m/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
[
![
](
https://img.shields.io/github/contributors-anon/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
...
...
@@ -15,7 +15,7 @@
![](
https://img.shields.io/badge/QQ%20Group-655462259-blue.svg?style=social&logo=tencent-qq
)
[
![Gitter
](
https://badges.gitter.im/jxnu-liguobin/cs-summary-reflection.svg
)
](https://gitter.im/jxnu-liguobin/cs-summary-reflection?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
[
中文
](
https://github.com/jxnu-liguobin/cs-summary-reflection/blob/master
/README.md
)
| English
[
中文
](
.
/README.md
)
| English
# Leetcode teamed up
...
...
README.md
浏览文件 @
aed4a1f3
...
...
@@ -6,8 +6,8 @@
[
![Security Rating
](
https://sonarcloud.io/api/project_badges/measure?project=jxnu-liguobin_cs-summary-reflection&metric=security_rating
)
](https://sonarcloud.io/dashboard?id=jxnu-liguobin_cs-summary-reflection)
[
![
](
https://img.shields.io/github/languages/count/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
[
![
](
https://img.shields.io/github/languages/top/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
[
![
](
https://img.shields.io/lgtm/alerts/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
dreamylost.cn
)
[
![
](
https://img.shields.io/lgtm/grade/python/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
dreamylost.c
n)
[
![
](
https://img.shields.io/lgtm/alerts/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
lgtm.com/projects/g/jxnu-liguobin/cs-summary-reflection/alerts/?mode=list
)
[
![
](
https://img.shields.io/lgtm/grade/python/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://
lgtm.com/projects/g/jxnu-liguobin/cs-summary-reflection/context:pytho
n)
[
![Language grade: Java
](
https://img.shields.io/lgtm/grade/java/g/jxnu-liguobin/cs-summary-reflection.svg?logo=lgtm&logoWidth=18
)
](https://lgtm.com/projects/g/jxnu-liguobin/cs-summary-reflection/context:java)
[
![
](
https://img.shields.io/github/commit-activity/m/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
[
![
](
https://img.shields.io/github/contributors-anon/jxnu-liguobin/cs-summary-reflection
)
](https://dreamylost.cn)
...
...
@@ -15,7 +15,7 @@
![](
https://img.shields.io/badge/QQ%20Group-655462259-blue.svg?style=social&logo=tencent-qq
)
[
![Gitter
](
https://badges.gitter.im/jxnu-liguobin/cs-summary-reflection.svg
)
](https://gitter.im/jxnu-liguobin/cs-summary-reflection?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge)
中文 |
[
English
](
https://github.com/jxnu-liguobin/cs-summary-reflection/blob/master
/README-en.md
)
中文 |
[
English
](
.
/README-en.md
)
# Leetcode 组队刷题
...
...
java-leetcode/src/main/java/io/github/wkk/everyday/aug/RecoverBinarySearchTree.java
0 → 100644
浏览文件 @
aed4a1f3
/* All Contributors (C) 2020 */
package
io.github.wkk.everyday.aug
;
import
io.github.wkk.structs.TreeNode
;
import
java.util.ArrayDeque
;
import
java.util.Deque
;
/**
* 恢复二叉搜索树
*
* <p>难点: 找出真正出问题的两个点
*
* <p>思路: 根据其性质, 使用中序遍历, 标记出真正出现问题的两个位置, 之后交换其元素值即可
*
* <p>抽象: .....errorOne |increasing sequence | errorTwo .......
*
* <p>第一个节点,是第一个按照中序遍历时候前一个节点大于后一个节点,我们选取前一个节点
*
* <p>第二个节点,是在第一个节点找到之后, 后面出现前一个节点大于后一个节点,我们选择后一个节点
*
* @author kongwiki@163.com
* @since 2020/8/8上午8:59
*/
public
class
RecoverBinarySearchTree
{
private
TreeNode
errorOne
,
errorTwo
;
public
void
recoverTree
(
TreeNode
root
)
{
helper
(
root
);
swap
(
errorOne
,
errorTwo
);
}
private
void
helper
(
TreeNode
root
)
{
if
(
root
==
null
)
{
return
;
}
TreeNode
pre
=
null
;
Deque
<
TreeNode
>
stack
=
new
ArrayDeque
<>();
TreeNode
p
=
root
;
while
(!
stack
.
isEmpty
()
||
p
!=
null
)
{
if
(
p
!=
null
)
{
stack
.
push
(
p
);
p
=
p
.
left
;
}
else
{
p
=
stack
.
pop
();
if
(
pre
!=
null
&&
pre
.
val
>
p
.
val
)
{
if
(
errorOne
==
null
)
{
errorOne
=
pre
;
errorTwo
=
p
;
}
else
{
errorTwo
=
p
;
}
}
pre
=
p
;
p
=
p
.
right
;
}
}
}
private
void
swap
(
TreeNode
first
,
TreeNode
second
)
{
int
temp
=
first
.
val
;
first
.
val
=
second
.
val
;
second
.
val
=
temp
;
}
}
java-leetcode/src/main/java/io/github/wkk/everyday/aug/RestoreIPAddresses.java
0 → 100644
浏览文件 @
aed4a1f3
/* All Contributors (C) 2020 */
package
io.github.wkk.everyday.aug
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* 复原IP地址(IP格式: 每一位都在0-255之间)
*
* <p>思路: 1.暴力求解 2.回溯(涉及到求所有的结果, 常用方法)
*
* @author kongwiki@163.com
* @since 2020/8/9上午9:33
*/
public
class
RestoreIPAddresses
{
/** 暴力求解 需要一个辅助方法, 判断每个子串是否符合IP格式 1. 每位在0-255之间 2. 不能出现0开头的两位以上数字 */
public
List
<
String
>
restoreIpAddresses
(
String
s
)
{
List
<
String
>
res
=
new
ArrayList
<>();
if
(
s
==
null
||
s
.
length
()
==
0
||
s
.
length
()
>
12
)
{
return
res
;
}
int
n
=
s
.
length
();
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
for
(
int
j
=
i
+
1
;
j
<
i
+
4
;
j
++)
{
for
(
int
k
=
j
+
1
;
k
<
j
+
4
;
k
++)
{
if
(
j
<
n
&&
k
<
n
)
{
String
a
=
s
.
substring
(
0
,
i
+
1
);
String
b
=
s
.
substring
(
i
+
1
,
j
+
1
);
String
c
=
s
.
substring
(
j
+
1
,
k
+
1
);
String
d
=
s
.
substring
(
k
+
1
);
if
(
helper
(
a
)
&&
helper
(
b
)
&&
helper
(
c
)
&&
helper
(
d
))
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
a
)
.
append
(
"."
)
.
append
(
b
)
.
append
(
"."
)
.
append
(
c
)
.
append
(
"."
)
.
append
(
d
);
res
.
add
(
sb
.
toString
());
}
}
}
}
}
return
res
;
}
private
boolean
helper
(
String
s
)
{
if
(
s
==
null
||
s
.
length
()
==
0
||
s
.
length
()
>
3
||
s
.
charAt
(
0
)
==
'0'
&&
s
.
length
()
>
1
||
Integer
.
parseInt
(
s
)
>
255
)
{
return
false
;
}
return
true
;
}
/** 回溯 */
public
List
<
String
>
restoreIpAddressesII
(
String
s
)
{
List
<
String
>
res
=
new
ArrayList
<>();
if
(
s
==
null
||
s
.
length
()
==
0
||
s
.
length
()
>
12
)
{
return
res
;
}
int
n
=
s
.
length
();
backtrack
(
0
,
""
,
4
,
s
,
n
,
res
);
return
res
;
}
/** 回溯本体 */
private
void
backtrack
(
int
start
,
String
tmp
,
int
flag
,
String
s
,
int
n
,
List
<
String
>
res
)
{
if
(
start
==
n
&&
flag
==
0
)
{
res
.
add
(
tmp
.
substring
(
0
,
tmp
.
length
()
-
1
));
return
;
}
if
(
flag
<
0
)
{
return
;
}
for
(
int
j
=
start
;
j
<
start
+
3
;
j
++)
{
if
(
j
<
n
)
{
if
(
start
==
j
&&
s
.
charAt
(
j
)
==
'0'
)
{
backtrack
(
j
+
1
,
tmp
+
s
.
charAt
(
j
)
+
"."
,
flag
-
1
,
s
,
n
,
res
);
break
;
}
if
(
Integer
.
parseInt
(
s
.
substring
(
start
,
j
+
1
))
<=
255
)
{
backtrack
(
j
+
1
,
tmp
+
s
.
substring
(
start
,
j
+
1
)
+
"."
,
flag
-
1
,
s
,
n
,
res
);
}
}
}
}
}
java-leetcode/src/main/java/io/github/wkk/everyday/aug/SameTree.java
0 → 100644
浏览文件 @
aed4a1f3
/* All Contributors (C) 2020 */
package
io.github.wkk.everyday.aug
;
import
io.github.wkk.structs.TreeNode
;
/**
* @author kongwiki@163.com
* @since 2020/8/7上午8:58
*/
public
class
SameTree
{
public
boolean
isSameTree
(
TreeNode
p
,
TreeNode
q
)
{
if
(
p
==
null
&&
q
==
null
)
{
return
true
;
}
else
if
(
p
==
null
||
q
==
null
)
{
return
false
;
}
else
if
(
p
.
val
!=
q
.
val
)
{
return
false
;
}
else
{
return
isSameTree
(
p
.
left
,
q
.
left
)
&&
isSameTree
(
p
.
right
,
q
.
right
);
}
}
}
java-leetcode/src/main/java/io/github/wkk/wkk.md
浏览文件 @
aed4a1f3
...
...
@@ -9,7 +9,9 @@
4.
[
207. 课程表
](
./everyday/aug/CourseSchedule.java
)
5.
[
337. 打家劫舍III
](
./everyday/aug/HouseRobberIII.java
)
6.
[
336. 回文对
](
./everyday/aug/PalindromePairs.java
)
7.
[
100. 相同的树
](
./everyday/aug/SameTree.java
)
8.
[
99. 恢复二叉搜索树
](
./everyday/aug/RecoverBinarySearchTree.java
)
9.
[
93. 复原IP地址
](
./everyday/aug/RestoreIPAddresses.java
)
## 哈希表
1.
[
1. 两数之和
](
./TwoSum.java
)
rust-leetcode/README.md
浏览文件 @
aed4a1f3
...
...
@@ -20,6 +20,7 @@ Leetcode Rust 实现
*
[
687 最长同值路径
](
src/leetcode_687.rs
)
*
[
257 二叉树的所有路径
](
src/leetcode_257.rs
)
*
[
1443 收集树上所有苹果的最少时间
](
src/leetcode_1443.rs
)
*
[
515 在每个树行中找最大值
](
src/leetcode_515.rs
)
### unsafe写法
...
...
rust-leetcode/src/interview_55_1.rs
浏览文件 @
aed4a1f3
...
...
@@ -6,11 +6,13 @@ use crate::pre_structs::{Solution, TreeNode};
///二叉树的深度 = leetcode 104
impl
Solution
{
//0 ms,100.00%
//2.7 MB,70.37%
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
;
let
node
=
root
.
borrow
();
return
max
(
get_depth
(
&
(
node
.left
)),
get_depth
(
&
(
node
.right
)
))
+
1
;
}
else
{
return
0
;
}
...
...
rust-leetcode/src/leetcode_107.rs
浏览文件 @
aed4a1f3
...
...
@@ -10,6 +10,9 @@ use crate::pre_structs::{Solution, TreeNode};
/// 例如:
/// 给定二叉树 [3,9,20,null,null,15,7],
impl
Solution
{
//应该用513那种层序
//0 ms,100.00%
//2.2 MB,33.33%
pub
fn
level_order_bottom
(
root
:
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
Vec
<
Vec
<
i32
>>
{
let
mut
ret
=
Vec
::
new
();
let
mut
node_level
=
VecDeque
::
new
();
...
...
@@ -20,12 +23,12 @@ impl Solution {
let
curr_node
=
node_level
.pop_front
();
if
let
Some
(
node
)
=
curr_node
{
if
let
Some
(
n
)
=
node
{
level
.push
(
n
.
try_borrow
()
.unwrap
()
.val
);
if
n
.
try_borrow
()
.unwrap
()
.left
.is_some
()
{
node_level
.push_back
(
n
.
try_borrow
()
.unwrap
()
.left
.clone
());
level
.push
(
n
.
as_ref
()
.borrow
()
.val
);
if
n
.
as_ref
()
.borrow
()
.left
.is_some
()
{
node_level
.push_back
(
n
.
as_ref
()
.borrow
()
.left
.clone
());
}
if
n
.
try_borrow
()
.unwrap
()
.right
.is_some
()
{
node_level
.push_back
(
n
.
try_borrow
()
.unwrap
()
.right
.clone
());
if
n
.
as_ref
()
.borrow
()
.right
.is_some
()
{
node_level
.push_back
(
n
.
as_ref
()
.borrow
()
.right
.clone
());
}
if
let
Some
(
f
)
=
flag
.borrow
()
{
if
f
.as_ptr
()
==
n
.as_ptr
()
{
...
...
rust-leetcode/src/leetcode_513.rs
0 → 100644
浏览文件 @
aed4a1f3
use
std
::
cell
::
RefCell
;
use
std
::
cmp
::
max
;
use
std
::
collections
::
VecDeque
;
use
std
::
rc
::
Rc
;
use
crate
::
pre_structs
::{
Solution
,
TreeNode
};
///513. 找树左下角的值
///给定一个二叉树,在树的最后一行找到最左边的值。
impl
Solution
{
//暴力
//0 ms,100.00%
//3 MB,100.00%
pub
fn
find_bottom_left_value
(
root
:
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
i32
{
fn
max_depth
(
root
:
&
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
i32
{
if
let
Some
(
root
)
=
root
{
let
node
=
root
.borrow
();
return
max
(
max_depth
(
&
(
node
.left
)),
max_depth
(
&
(
node
.right
)))
+
1
;
}
else
{
return
0
;
}
}
let
depth
=
max_depth
(
&
root
);
let
mut
level
=
1
;
let
mut
node_level
=
VecDeque
::
new
();
node_level
.push_back
(
root
.clone
());
while
!
node_level
.is_empty
()
{
let
size
=
node_level
.len
();
for
i
in
0
..
size
{
let
curr_node
=
node_level
.pop_front
();
if
let
Some
(
node
)
=
curr_node
{
if
let
Some
(
n
)
=
node
{
if
i
==
0
&&
level
==
depth
{
return
n
.borrow
()
.val
;
}
if
n
.borrow
()
.left
.is_some
()
{
node_level
.push_back
(
n
.borrow
()
.left
.clone
());
}
if
n
.borrow
()
.right
.is_some
()
{
node_level
.push_back
(
n
.borrow
()
.right
.clone
());
}
}
}
}
level
+=
1
;
}
0
}
//从右开始的层序,最后出队列的元素就是最左的
//0 ms,100.00%
//2.9 MB,100.00%
pub
fn
find_bottom_left_value2
(
root
:
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
i32
{
let
mut
node_level
=
VecDeque
::
new
();
node_level
.push_back
(
root
.clone
());
let
mut
ret
:
Option
<
Rc
<
RefCell
<
TreeNode
>>>
=
None
;
while
!
node_level
.is_empty
()
{
//题目要求树不能为空,队列元素都是非空
let
mut
node
=
node_level
.pop_front
()
.unwrap
();
ret
=
node
.clone
();
if
let
Some
(
node
)
=
node
{
if
node
.borrow
()
.right
.is_some
()
{
node_level
.push_back
(
node
.borrow
()
.right
.clone
());
}
if
node
.borrow
()
.left
.is_some
()
{
node_level
.push_back
(
node
.borrow
()
.left
.clone
());
}
}
}
ret
.unwrap
()
.as_ref
()
.borrow
()
.val
}
}
#[cfg(test)]
mod
test
{
use
std
::
cell
::
RefCell
;
use
std
::
rc
::
Rc
;
use
crate
::
pre_structs
::{
Solution
,
TreeNode
};
#[test]
fn
find_bottom_left_value
()
{
let
e2
=
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
2
,
left
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
1
,
left
:
None
,
right
:
None
,
}))),
right
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
3
,
left
:
None
,
right
:
None
,
}))),
})));
let
ret
=
Solution
::
find_bottom_left_value
(
e2
.clone
());
let
ret2
=
Solution
::
find_bottom_left_value
(
e2
);
assert
!
(
ret
==
1
);
assert
!
(
ret2
==
1
);
println!
(
"{}"
,
ret
);
println!
(
"{}"
,
ret2
);
}
}
rust-leetcode/src/leetcode_515.rs
0 → 100644
浏览文件 @
aed4a1f3
use
std
::
cell
::
RefCell
;
use
std
::
cmp
::
max
;
use
std
::
collections
::
VecDeque
;
use
std
::
rc
::
Rc
;
use
crate
::
pre_structs
::{
Solution
,
TreeNode
};
///515. 在每个树行中找最大值
///您需要在二叉树的每一行中找到最大的值。
impl
Solution
{
//0 ms,100.00%
//3 MB,100.00%
pub
fn
largest_values
(
root
:
Option
<
Rc
<
RefCell
<
TreeNode
>>>
)
->
Vec
<
i32
>
{
if
let
None
=
root
{
return
vec!
[]
.to_vec
();
}
let
mut
node_level
:
VecDeque
<
Option
<
Rc
<
RefCell
<
TreeNode
>>>>
=
VecDeque
::
new
();
node_level
.push_back
(
root
);
let
mut
ret
=
Vec
::
<
i32
>
::
new
();
let
mut
max_val
=
i32
::
min_value
();
while
!
node_level
.is_empty
()
{
let
size
=
node_level
.len
();
for
i
in
0
..
size
{
let
curr_node
=
node_level
.pop_front
();
if
let
Some
(
node
)
=
curr_node
{
//应该尽量避免使用unwrap
if
let
Some
(
n
)
=
node
{
max_val
=
max
(
max_val
,
n
.borrow
()
.val
);
if
n
.borrow
()
.left
.is_some
()
{
node_level
.push_back
(
n
.borrow
()
.left
.clone
());
}
if
n
.borrow
()
.right
.is_some
()
{
node_level
.push_back
(
n
.borrow
()
.right
.clone
());
}
}
}
}
ret
.push
(
max_val
);
max_val
=
i32
::
min_value
();
}
ret
}
}
#[cfg(test)]
mod
test
{
use
std
::
cell
::
RefCell
;
use
std
::
rc
::
Rc
;
use
crate
::
pre_structs
::{
print_vec
,
Solution
,
TreeNode
};
#[test]
fn
largest_values
()
{
let
e1
=
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
1
,
left
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
3
,
left
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
5
,
left
:
None
,
right
:
None
,
}))),
right
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
3
,
left
:
None
,
right
:
None
,
}))),
}))),
right
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
2
,
left
:
None
,
right
:
Some
(
Rc
::
new
(
RefCell
::
new
(
TreeNode
{
val
:
9
,
left
:
None
,
right
:
None
,
}))),
}))),
})));
let
ret
=
Solution
::
largest_values
(
e1
.clone
());
print_vec
(
ret
);
}
}
rust-leetcode/src/main.rs
浏览文件 @
aed4a1f3
...
...
@@ -58,6 +58,8 @@ pub mod leetcode_38;
pub
mod
leetcode_461
;
pub
mod
leetcode_475
;
pub
mod
leetcode_500
;
pub
mod
leetcode_513
;
pub
mod
leetcode_515
;
pub
mod
leetcode_532
;
pub
mod
leetcode_557
;
pub
mod
leetcode_561
;
...
...
scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_93.scala
0 → 100644
浏览文件 @
aed4a1f3
/* Licensed under Apache-2.0 (C) All Contributors */
package
io.github.sweeneycai
import
scala.collection.mutable.ListBuffer
/**
* 93. 复原IP地址 (Medium)
*
* 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。
* 有效的 IP 地址正好由四个整数(每个整数位于 0 到 255 之间组成),整数之间用 '.' 分隔。
*/
object
Leetcode_93
{
/**
* 简单的深度优先搜索,在 2.13.x 中`Stream`可以使用`LazyList`代替
* 注意考虑'0'作为开头时的特殊情况
*/
def
restoreIpAddresses
(
s
:
String
)
:
List
[
String
]
=
{
if
(
s
.
length
>
12
||
s
.
length
<
4
)
List
.
empty
[
String
]
else
{
(
for
{
i
<-
from
(
List
(
s
->
List
.
empty
[
String
]))
if
i
.
_1
==
""
&&
i
.
_2
.
length
==
4
}
yield
i
.
_2
).
map
(
_
.
mkString
(
"."
)).
toList
}
}
type
Path
=
(
String
,
List
[
String
])
def
generate
(
s
:
String
)
:
List
[(
String
,
String
)]
=
{
val
possible
=
ListBuffer
.
empty
[(
String
,
String
)]
if
(
s
.
length
>=
3
&&
(
s
.
charAt
(
0
)
==
'1'
||
(
s
.
charAt
(
0
)
==
'2'
&&
s
.
substring
(
0
,
3
)
<
"256"
))
)
{
possible
.
append
(
s
.
splitAt
(
3
))
}
if
(
s
.
length
>=
2
&&
s
.
charAt
(
0
)
!=
'0'
)
possible
.
append
(
s
.
splitAt
(
2
))
if
(
s
.
length
>=
1
)
possible
.
append
(
s
.
splitAt
(
1
))
possible
.
toList
}
def
next
(
s
:
String
,
history
:
List
[
String
])
:
List
[
Path
]
=
{
generate
(
s
)
.
foldLeft
(
List
.
empty
[
Path
])
{
(
acc
,
ss
)
=>
acc
:+
(
ss
.
_2
,
history
:+
ss
.
_1
)
}
.
filter
(
_
.
_2
.
length
<=
4
)
}
def
from
(
paths
:
List
[
Path
])
:
Stream
[
Path
]
=
if
(
paths
.
isEmpty
)
Stream
.
empty
else
{
val
more
=
for
{
path
<-
paths
next
<-
next
(
path
.
_1
,
path
.
_2
)
}
yield
next
from
(
more
).
append
(
paths
)
}
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
println
(
restoreIpAddresses
(
"25345244"
))
}
}
scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_99.scala
0 → 100644
浏览文件 @
aed4a1f3
/* Licensed under Apache-2.0 (C) All Contributors */
package
io.github.sweeneycai
import
io.github.dreamylost.TreeNode
import
scala.collection.mutable
/**
* 99. 恢复二叉搜索树 (Hard)
* 二叉搜索树中的两个节点被错误地交换。
*
* 在不改变其结构的情况下,恢复这棵树。
*/
object
Leetcode_99
extends
App
{
/**
* 非递归版本
*/
def
recoverTree
(
root
:
TreeNode
)
:
Unit
=
{
val
stack
:
mutable.Stack
[
TreeNode
]
=
mutable
.
Stack
()
var
pre
=
new
TreeNode
(
Int
.
MinValue
)
var
bigger
:
TreeNode
=
null
var
lower
:
TreeNode
=
null
var
cur
=
root
while
(
stack
.
nonEmpty
||
cur
!=
null
)
{
if
(
cur
!=
null
)
{
stack
.
push
(
cur
)
cur
=
cur
.
left
}
else
{
cur
=
stack
.
pop
()
if
(
cur
.
value
<
pre
.
value
&&
bigger
==
null
)
{
bigger
=
pre
lower
=
cur
}
else
if
(
cur
.
value
<
pre
.
value
)
{
lower
=
cur
}
pre
=
cur
cur
=
cur
.
right
}
}
if
(
bigger
!=
null
&&
lower
!=
null
)
{
val
value
=
bigger
.
value
bigger
.
value
=
lower
.
value
lower
.
value
=
value
}
}
/**
* 递归版本
*/
def
recoverTreeWithRecur
(
root
:
TreeNode
)
:
Unit
=
{
if
(
root
==
null
)
return
var
pre
=
new
TreeNode
(
Int
.
MinValue
)
var
bigger
:
TreeNode
=
null
var
lower
:
TreeNode
=
null
def
helper
(
node
:
TreeNode
)
:
Unit
=
{
if
(
node
.
left
!=
null
)
helper
(
node
.
left
)
if
(
node
.
value
<
pre
.
value
)
{
lower
=
node
if
(
bigger
==
null
)
bigger
=
pre
}
pre
=
node
if
(
node
.
right
!=
null
)
helper
(
node
.
right
)
}
helper
(
root
)
val
value
=
bigger
.
value
bigger
.
value
=
lower
.
value
lower
.
value
=
value
}
val
root
=
new
TreeNode
(
3
)
root
.
left
=
new
TreeNode
(
1
)
root
.
right
=
new
TreeNode
(
4
)
root
.
right
.
left
=
new
TreeNode
(
2
)
recoverTree
(
root
)
println
(
root
)
}
scala-leetcode/src/main/scala/io/github/sweeneycai/sweeneycai.md
浏览文件 @
aed4a1f3
...
...
@@ -2,9 +2,14 @@
-
[
707.设计链表
](
./Leetcode_707.scala
)
### 树
-
[
99.恢复二叉搜索树
](
./Leetcode_99.scala
)
### 深度优先搜索
-
[
365.水壶问题
](
./Leetcode_365.scala
)
-
[
93. 复原IP地址
](
./Leetcode_93.scala
)
### 其他
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录