Next Greater Element II LeetCode solution in C++
1class Solution {2public:3vector<int> nextGreaterElements(vector<int> &nums) {4int n = nums.size();5vector<int> res;67for (int i = 0; i < n; i++) {8int j = i + 1;9if (j == n) j = 0;10int next = -1;1112while (j != i) {13if (nums[j] > nums[i]) {14next = nums[j];15break;16}17j++;18if (j == n) j = 0;19}20res.push_back(next);21}22return res;23}24};