File tree 3 files changed +61
-0
lines changed
solution/0400-0499/0496.Next Greater Element I
3 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -99,6 +99,28 @@ class Solution {
99
99
}
100
100
```
101
101
102
+ ### ** JavaScript**
103
+
104
+ ``` js
105
+ /**
106
+ * @param {number[]} nums1
107
+ * @param {number[]} nums2
108
+ * @return {number[]}
109
+ */
110
+ var nextGreaterElement = function (nums1 , nums2 ) {
111
+ let stack = [];
112
+ let nextBigger = {};
113
+ for (let num of nums2) {
114
+ while (stack .length > 0 && stack[stack .length - 1 ] < num) {
115
+ nextBigger[stack .pop ()] = num;
116
+ }
117
+ stack .push (num);
118
+ }
119
+ let res = nums1 .map (d => nextBigger[d] || - 1 );
120
+ return res;
121
+ };
122
+ ```
123
+
102
124
### ** ...**
103
125
104
126
```
Original file line number Diff line number Diff line change @@ -84,6 +84,28 @@ class Solution {
84
84
}
85
85
```
86
86
87
+ ### ** JavaScript**
88
+
89
+ ``` js
90
+ /**
91
+ * @param {number[]} nums1
92
+ * @param {number[]} nums2
93
+ * @return {number[]}
94
+ */
95
+ var nextGreaterElement = function (nums1 , nums2 ) {
96
+ let stack = [];
97
+ let nextBigger = {};
98
+ for (let num of nums2) {
99
+ while (stack .length > 0 && stack[stack .length - 1 ] < num) {
100
+ nextBigger[stack .pop ()] = num;
101
+ }
102
+ stack .push (num);
103
+ }
104
+ let res = nums1 .map (d => nextBigger[d] || - 1 );
105
+ return res;
106
+ };
107
+ ```
108
+
87
109
### ** ...**
88
110
89
111
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums1
3
+ * @param {number[] } nums2
4
+ * @return {number[] }
5
+ */
6
+ var nextGreaterElement = function ( nums1 , nums2 ) {
7
+ let stack = [ ] ;
8
+ let nextBigger = { } ;
9
+ for ( let num of nums2 ) {
10
+ while ( stack . length > 0 && stack [ stack . length - 1 ] < num ) {
11
+ nextBigger [ stack . pop ( ) ] = num ;
12
+ }
13
+ stack . push ( num ) ;
14
+ }
15
+ let res = nums1 . map ( d => nextBigger [ d ] || - 1 ) ;
16
+ return res ;
17
+ } ;
You can’t perform that action at this time.
0 commit comments