CodeLibrary/01_Clion_Cplus_learn_20240419/main.cpp

54 lines
1.0 KiB
C++

#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
vector<int> ret;
int ns = nums.size();
for (int i = 0; i < ns; i++){
int j;
if (i < nums.size() - 1){
j = i + 1;
} else {
j = 0;
}
while (true){
if (nums[i] < nums[j]){
ret.push_back(nums[j]);
break;
}
if (j < nums.size() - 1){
j++;
} else {
j = 0;
}
if (i == j){
ret.push_back(-1);
break;
}
}
}
return ret;
}
};
int main() {
// vector<int> v = {1, 2, 3, 4, 3};
vector<int> v = {1, 2, 1};
Solution s_1;
vector<int> vs = s_1.nextGreaterElements(v);
for (auto array: vs){
cout << array << " ";
}
return 0;
}