Merge pull request #586 from caozheng0401/master

添加 496.下一个更大元素 Java方法
This commit is contained in:
程序员Carl
2021-08-11 14:50:46 +08:00
committed by GitHub
2 changed files with 30 additions and 1 deletions

View File

@ -186,7 +186,37 @@ public:
建议大家把情况一二三想清楚了,先写出版本一的代码,然后在其基础上在做精简!
## 其他语言版本
Java
```java
class Solution {
public int[] nextGreaterElement(int[] nums1, int[] nums2) {
Stack<Integer> temp = new Stack<>();
int[] res = new int[nums1.length];
Arrays.fill(res,-1);
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0 ; i< nums1.length ; i++){
hashMap.put(nums1[i],i);
}
temp.add(0);
for (int i = 1; i < nums2.length; i++) {
if (nums2[i] <= nums2[temp.peek()]) {
temp.add(i);
} else {
while (!temp.isEmpty() && nums2[temp.peek()] < nums2[i]) {
if (hashMap.containsKey(nums2[temp.peek()])){
Integer index = hashMap.get(nums2[temp.peek()]);
res[index] = nums2[i];
}
temp.pop();
}
temp.add(i);
}
}
return res;
}
}
```
Python
```python3
class Solution:

View File

@ -95,7 +95,6 @@ public:
## 其他语言版本
Java:
```Java
class Solution {
public int[] nextGreaterElements(int[] nums) {