Next Greater Element II LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> nextGreaterElements(vector<int> &nums) {
4
int n = nums.size();
5
vector<int> res;
6

7
for (int i = 0; i < n; i++) {
8
int j = i + 1;
9
if (j == n) j = 0;
10
int next = -1;
11

12
while (j != i) {
13
if (nums[j] > nums[i]) {
14
next = nums[j];
15
break;
16
}
17
j++;
18
if (j == n) j = 0;
19
}
20
res.push_back(next);
21
}
22
return res;
23
}
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0