Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
CS-Notes
提交
47b6ed62
C
CS-Notes
项目概览
wushizhenking
/
CS-Notes
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
CS-Notes
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
47b6ed62
编写于
6月 08, 2018
作者:
C
CyC2018
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
auto commit
上级
34072ecf
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
18 addition
and
32 deletion
+18
-32
notes/Leetcode 题解.md
notes/Leetcode 题解.md
+18
-32
未找到文件。
notes/Leetcode 题解.md
浏览文件 @
47b6ed62
...
...
@@ -3992,48 +3992,34 @@ public int[] dailyTemperatures(int[] temperatures) {
}
```
**在另一个数组中比当前元素大的下一个元素**
[
496. Next Greater Element I (Easy)
](
https://leetcode.com/problems/next-greater-element-i/description/
)
**循环数组中比当前元素大的下一个元素**
```
html
Input: nums1 = [4,1,2], nums2 = [1,3,4,2].
Output: [-1,3,-1]
```
[
503. Next Greater Element II (Medium)
](
https://leetcode.com/problems/next-greater-element-ii/description/
)
```
java
public
int
[]
nextGreaterElement
(
int
[]
nums1
,
int
[]
nums2
)
{
Map
<
Integer
,
Integer
>
map
=
new
HashMap
<>();
Stack
<
Integer
>
stack
=
new
Stack
<>();
for
(
int
num
:
nums2
)
{
while
(!
stack
.
isEmpty
()
&&
num
>
stack
.
peek
())
{
map
.
put
(
stack
.
pop
(),
num
);
}
stack
.
add
(
num
);
}
int
[]
ret
=
new
int
[
nums1
.
length
];
for
(
int
i
=
0
;
i
<
nums1
.
length
;
i
++)
{
if
(
map
.
containsKey
(
nums1
[
i
]))
ret
[
i
]
=
map
.
get
(
nums1
[
i
]);
else
ret
[
i
]
=
-
1
;
}
return
ret
;
}
```
text
Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
```
**循环数组中比当前元素大的下一个元素**
[
503. Next Greater Element II (Medium)
](
https://leetcode.com/problems/next-greater-element-ii/description/
)
与 739. Daily Temperatures (Medium) 不同的是,数组是循环数组,并且最后要求的不是距离而是下一个元素。
```
java
public
int
[]
nextGreaterElements
(
int
[]
nums
)
{
int
n
=
nums
.
length
,
next
[]
=
new
int
[
n
];
int
n
=
nums
.
length
;
int
[]
next
=
new
int
[
n
];
Arrays
.
fill
(
next
,
-
1
);
Stack
<
Integer
>
stack
=
new
Stack
<>();
Stack
<
Integer
>
pre
=
new
Stack
<>();
for
(
int
i
=
0
;
i
<
n
*
2
;
i
++)
{
int
num
=
nums
[
i
%
n
];
while
(!
stack
.
isEmpty
()
&&
nums
[
stack
.
peek
()]
<
num
)
next
[
stack
.
pop
()]
=
num
;
if
(
i
<
n
)
stack
.
push
(
i
);
while
(!
pre
.
isEmpty
()
&&
nums
[
pre
.
peek
()]
<
num
)
{
next
[
pre
.
pop
()]
=
num
;
}
if
(
i
<
n
){
pre
.
push
(
i
);
}
}
return
next
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录