Next Greater Element III LeetCode solution in C++

1
class Solution {
2
public:
3
int nextGreaterElement(int n) {
4
vector<int> vec;
5
int temp = n;
6
while (n > 0) {
7
int r = n % 10;
8
vec.push_back(r);
9
n /= 10;
10
}
11
sort(vec.begin(), vec.end());
12
do {
13
int num = 0;
14
long j = 0;
15
int s = vec.size() - 1;
16
long i = pow(10, s);
17
while (i > 0) {
18
num += i * vec[j++];
19
i /= 10;
20
}
21
if (num > temp) return num;
22

23
} while (next_permutation(vec.begin(), vec.end()));
24
return -1;
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0