CodeLibrary/01_Clion_Cplus_learn_20240419/main.cpp

54 lines
1.0 KiB
C++
Raw Normal View History

2024-04-20 00:40:58 +08:00
#include <iostream>
#include <vector>
2024-04-20 00:40:58 +08:00
using namespace std;
class Solution {
2024-04-20 00:40:58 +08:00
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;
}
2024-04-20 00:40:58 +08:00
while (true){
if (nums[i] < nums[j]){
ret.push_back(nums[j]);
break;
}
2024-04-20 00:40:58 +08:00
if (j < nums.size() - 1){
j++;
} else {
j = 0;
}
if (i == j){
ret.push_back(-1);
break;
}
}
}
return ret;
}
2024-04-20 00:40:58 +08:00
};
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 << " ";
}
2024-04-20 00:40:58 +08:00
return 0;
}