-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode496.java
More file actions
89 lines (78 loc) · 2.53 KB
/
Copy pathleetcode496.java
File metadata and controls
89 lines (78 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package leetcode;
import java.util.HashMap;
import java.util.Stack;
public class leetcode496 {
class Solution {
//贪心法
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
int [] result=new int[nums1.length];
for(int i=0;i<nums1.length;i++){
for(int j=0;j<nums2.length;j++){
if(nums2[j]==nums1[i]){
for(int p=j+1;p<nums2.length;p++) {
if (nums2[p] > nums1[i]) {
result[i] = nums2[p];
break;
}
}
}
}
}
for(int i=0;i<nums1.length;i++){
if(result[i]==0) result[i]=-1;
}
return result;
}
//单调栈
public int[] SnextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> s = null;
int[] res = new int[nums1.length];
for(int i=0;i<nums1.length;i++){
res[i] = -1;
s = new Stack<Integer>();
for(int m:nums2){
s.push(m);
}
while(!s.empty()&&s.peek()!=nums1[i]){
if(s.peek()>nums1[i]){
res[i] = s.pop();
}else{
s.pop();
}
}
}
return res;
}
//只用栈
public int[] S2nextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> stack = new Stack<>();
HashMap<Integer,Integer> maps = new HashMap<Integer,Integer>();
for(int i = 0;i<nums2.length;i++){
while(!stack.isEmpty() && stack.peek()<nums2[i]){
maps.put(stack.pop(),nums2[i]);
}
stack.push(nums2[i]);
}
int[] result = new int[nums1.length];
for(int i = 0;i<nums1.length;i++){
result[i] = maps.getOrDefault(nums1[i],-1);
}
return result;
}
}
public static void main(String[] args) {
new leetcode496().new Solution().S2nextGreaterElement(
new int[]{
4,1,2
},
new int[]{
1,3,4,2,6,8
}
);
}
}
/*
*
* [3,1,5,7,9,2,6]
[1,2,3,5,6,7,9,11]
* */