File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Expand file tree Collapse file tree 2 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -186,7 +186,37 @@ public:
186
186
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
187
187
188
188
## 其他语言版本
189
+ Java
190
+ ``` java
191
+ class Solution {
192
+ public int [] nextGreaterElement (int [] nums1 , int [] nums2 ) {
193
+ Stack<Integer > temp = new Stack<> ();
194
+ int [] res = new int [nums1. length];
195
+ Arrays . fill(res,- 1 );
196
+ HashMap<Integer , Integer > hashMap = new HashMap<> ();
197
+ for (int i = 0 ; i< nums1. length ; i++ ){
198
+ hashMap. put(nums1[i],i);
199
+ }
200
+ temp. add(0 );
201
+ for (int i = 1 ; i < nums2. length; i++ ) {
202
+ if (nums2[i] <= nums2[temp. peek()]) {
203
+ temp. add(i);
204
+ } else {
205
+ while (! temp. isEmpty() && nums2[temp. peek()] < nums2[i]) {
206
+ if (hashMap. containsKey(nums2[temp. peek()])){
207
+ Integer index = hashMap. get(nums2[temp. peek()]);
208
+ res[index] = nums2[i];
209
+ }
210
+ temp. pop();
211
+ }
212
+ temp. add(i);
213
+ }
214
+ }
189
215
216
+ return res;
217
+ }
218
+ }
219
+ ```
190
220
Python:
191
221
``` python3
192
222
class Solution :
Original file line number Diff line number Diff line change @@ -95,7 +95,6 @@ public:
95
95
## 其他语言版本
96
96
97
97
Java:
98
-
99
98
```Java
100
99
class Solution {
101
100
public int[] nextGreaterElements(int[] nums) {
You can’t perform that action at this time.
0 commit comments