Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8fad926

Browse files
更新 0496.下一个更大元素I.md Java代码
添加另一种思路的Java代码,更加清晰简洁
1 parent 09d3e94 commit 8fad926

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

‎problems/0496.下一个更大元素I.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,32 @@ class Solution {
221221
return res;
222222
}
223223
}
224+
225+
// 版本2
226+
class Solution {
227+
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
228+
HashMap<Integer, Integer> map = new HashMap<>();
229+
for (int i = 0; i < nums1.length; i++) {
230+
map.put(nums1[i], i);
231+
}
232+
233+
int[] res = new int[nums1.length];
234+
Stack<Integer> stack = new Stack<>();
235+
Arrays.fill(res, -1);
236+
237+
for (int i = 0; i < nums2.length; i++) {
238+
while (!stack.isEmpty() && nums2[stack.peek()] < nums2[i]) {
239+
int pre = nums2[stack.pop()];
240+
if (map.containsKey(pre)) {
241+
res[map.get(pre)] = nums2[i];
242+
}
243+
}
244+
stack.push(i);
245+
}
246+
247+
return res;
248+
}
249+
}
224250
```
225251
Python3:
226252
```python

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /