File tree 4 files changed +79
-8
lines changed
solution/0400-0499/0496.Next Greater Element I
4 files changed +79
-8
lines changed Original file line number Diff line number Diff line change 40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
+ 先对将 nums2 中的每一个元素,求出其下一个更大的元素。随后对于将这些答案放入哈希映射(HashMap)中,再遍历数组 nums1,并直接找出答案。对于 nums2,可以使用单调栈来解决这个问题。
44
+
43
45
<!-- tabs:start -->
44
46
45
47
### ** Python3**
46
48
47
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
48
50
49
51
``` python
50
-
52
+ class Solution :
53
+ def nextGreaterElement (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
54
+ mapper = dict ()
55
+ stack = []
56
+ for num in nums2:
57
+ while stack and stack[- 1 ] < num:
58
+ mapper[stack.pop()] = num
59
+ stack.append(num)
60
+ res = []
61
+ for num in nums1:
62
+ res.append(mapper.get(num, - 1 ))
63
+ return res
51
64
```
52
65
53
66
### ** Java**
54
67
55
68
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
69
57
70
``` java
58
-
71
+ class Solution {
72
+ public int [] nextGreaterElement (int [] nums1 , int [] nums2 ) {
73
+ Deque<Integer > stack = new ArrayDeque<> ();
74
+ Map<Integer , Integer > map = new HashMap<> ();
75
+ for (int num : nums2) {
76
+ while (! stack. isEmpty() && stack. peek() < num) {
77
+ map. put(stack. pop(), num);
78
+ }
79
+ stack. push(num);
80
+ }
81
+ int n = nums1. length;
82
+ int [] res = new int [n];
83
+ for (int i = 0 ; i < n; ++ i) {
84
+ res[i] = map. getOrDefault(nums1[i], - 1 );
85
+ }
86
+ return res;
87
+ }
88
+ }
59
89
```
60
90
61
91
### ** ...**
Original file line number Diff line number Diff line change @@ -73,13 +73,41 @@ The Next Greater Number of a number <b>x</b> in <code>nums1</code> is the first
73
73
### ** Python3**
74
74
75
75
``` python
76
-
76
+ class Solution :
77
+ def nextGreaterElement (self , nums1 : List[int ], nums2 : List[int ]) -> List[int ]:
78
+ mapper = dict ()
79
+ stack = []
80
+ for num in nums2:
81
+ while stack and stack[- 1 ] < num:
82
+ mapper[stack.pop()] = num
83
+ stack.append(num)
84
+ res = []
85
+ for num in nums1:
86
+ res.append(mapper.get(num, - 1 ))
87
+ return res
77
88
```
78
89
79
90
### ** Java**
80
91
81
92
``` java
82
-
93
+ class Solution {
94
+ public int [] nextGreaterElement (int [] nums1 , int [] nums2 ) {
95
+ Deque<Integer > stack = new ArrayDeque<> ();
96
+ Map<Integer , Integer > map = new HashMap<> ();
97
+ for (int num : nums2) {
98
+ while (! stack. isEmpty() && stack. peek() < num) {
99
+ map. put(stack. pop(), num);
100
+ }
101
+ stack. push(num);
102
+ }
103
+ int n = nums1. length;
104
+ int [] res = new int [n];
105
+ for (int i = 0 ; i < n; ++ i) {
106
+ res[i] = map. getOrDefault(nums1[i], - 1 );
107
+ }
108
+ return res;
109
+ }
110
+ }
83
111
```
84
112
85
113
### ** ...**
Original file line number Diff line number Diff line change @@ -3,15 +3,16 @@ public int[] nextGreaterElement(int[] nums1, int[] nums2) {
3
3
Deque <Integer > stack = new ArrayDeque <>();
4
4
Map <Integer , Integer > map = new HashMap <>();
5
5
for (int num : nums2 ) {
6
- while (!stack .isEmpty () && num > stack .peek ()) {
6
+ while (!stack .isEmpty () && stack .peek () < num ) {
7
7
map .put (stack .pop (), num );
8
8
}
9
9
stack .push (num );
10
10
}
11
- int [] res = new int [nums1 .length ];
12
- for (int i = 0 ; i < nums1 .length ; ++i ) {
11
+ int n = nums1 .length ;
12
+ int [] res = new int [n ];
13
+ for (int i = 0 ; i < n ; ++i ) {
13
14
res [i ] = map .getOrDefault (nums1 [i ], -1 );
14
15
}
15
16
return res ;
16
17
}
17
- }
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def nextGreaterElement (self , nums1 : List [int ], nums2 : List [int ]) -> List [int ]:
3
+ mapper = dict ()
4
+ stack = []
5
+ for num in nums2 :
6
+ while stack and stack [- 1 ] < num :
7
+ mapper [stack .pop ()] = num
8
+ stack .append (num )
9
+ res = []
10
+ for num in nums1 :
11
+ res .append (mapper .get (num , - 1 ))
12
+ return res
You can’t perform that action at this time.
0 commit comments