Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wmsofts
Fucking Algorithm
提交
a1f5f499
F
Fucking Algorithm
项目概览
wmsofts
/
Fucking Algorithm
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
Fucking Algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a1f5f499
编写于
11月 10, 2020
作者:
F
FanFan0919
提交者:
labuladong
11月 17, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【42. 接雨水】【Java】
上级
0ad9a50f
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
97 addition
and
2 deletion
+97
-2
高频面试系列/接雨水.md
高频面试系列/接雨水.md
+97
-2
未找到文件。
高频面试系列/接雨水.md
浏览文件 @
a1f5f499
...
...
@@ -211,4 +211,99 @@ if (l_max < r_max) {
<img
src=
"../pictures/qrcode.jpg"
width=
200
>
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
[
Yifan Zhang
](
https://github.com/FanFan0919
)
提供 java 代码
**双指针解法**
:时间复杂度 O(N),空间复杂度 O(1)
对cpp版本的解法有非常微小的优化。
因为我们每次循环只会选 left 或者 right 处的柱子来计算,因此我们并不需要在每次循环中同时更新
`maxLeft`
和
`maxRight`
。
我们可以先比较
`maxLeft`
和
`maxRight`
,决定这次选择计算的柱子是
`height[left]`
或者
`height[right]`
后再更新对应的
`maxLeft`
或
`maxRight`
。
当然这并不会在时间上带来什么优化,只是提供一种思路。
```
java
class
Solution
{
public
int
trap
(
int
[]
height
)
{
if
(
height
==
null
||
height
.
length
==
0
)
return
0
;
int
left
=
0
,
right
=
height
.
length
-
1
;
int
maxLeft
=
height
[
left
],
maxRight
=
height
[
right
];
int
res
=
0
;
while
(
left
<
right
)
{
// 比较 maxLeft 和 maxRight,决定这次计算 left 还是 right 处的柱子
if
(
maxLeft
<
maxRight
)
{
left
++;
maxLeft
=
Math
.
max
(
maxLeft
,
height
[
left
]);
// update maxLeft
res
+=
maxLeft
-
height
[
left
];
}
else
{
right
--;
maxRight
=
Math
.
max
(
maxRight
,
height
[
right
]);
// update maxRight
res
+=
maxRight
-
height
[
right
];
}
}
return
res
;
}
}
```
附上暴力解法以及备忘录解法的 java 代码
**暴力解法**
:时间复杂度 O(N^2),空间复杂度 O(1)
```
java
class
Solution
{
public
int
trap
(
int
[]
height
)
{
if
(
height
==
null
||
height
.
length
==
0
)
return
0
;
int
n
=
height
.
length
;
int
res
=
0
;
// 跳过最左边和最右边的柱子,从第二个柱子开始
for
(
int
i
=
1
;
i
<
n
-
1
;
i
++)
{
int
maxLeft
=
0
,
maxRight
=
0
;
// 找右边最高的柱子
for
(
int
j
=
i
;
j
<
n
;
j
++)
{
maxRight
=
Math
.
max
(
maxRight
,
height
[
j
]);
}
// 找左边最高的柱子
for
(
int
j
=
i
;
j
>=
0
;
j
--)
{
maxLeft
=
Math
.
max
(
maxLeft
,
height
[
j
]);
}
// 如果自己就是最高的话,
// maxLeft == maxRight == height[i]
res
+=
Math
.
min
(
maxLeft
,
maxRight
)
-
height
[
i
];
}
return
res
;
}
}
```
**备忘录解法**
:时间复杂度 O(N),空间复杂度 O(N)
```
java
class
Solution
{
public
int
trap
(
int
[]
height
)
{
if
(
height
==
null
||
height
.
length
==
0
)
return
0
;
int
n
=
height
.
length
;
int
res
=
0
;
// 数组充当备忘录
int
[]
maxLeft
=
new
int
[
n
];
int
[]
maxRight
=
new
int
[
n
];
// 初始化 base case
maxLeft
[
0
]
=
height
[
0
];
maxRight
[
n
-
1
]
=
height
[
n
-
1
];
// 从左向右计算 maxLeft
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
maxLeft
[
i
]
=
Math
.
max
(
maxLeft
[
i
-
1
],
height
[
i
]);
}
// 从右向左计算 maxRight
for
(
int
i
=
n
-
2
;
i
>=
0
;
i
--)
{
maxRight
[
i
]
=
Math
.
max
(
maxRight
[
i
+
1
],
height
[
i
]);
}
// 计算答案
for
(
int
i
=
1
;
i
<
n
;
i
++)
{
res
+=
Math
.
min
(
maxLeft
[
i
],
maxRight
[
i
])
-
height
[
i
];
}
return
res
;
}
}
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录