-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path556.cpp
50 lines (34 loc) · 819 Bytes
/
556.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public:
int nextGreaterElement(int x) {
string A=to_string(x);
int n=A.length();
int l;
for(int i=n-1;i>0;i--){
if(A[i]>A[i-1]){
l=i;
break;
}
}
l=l-1;
if(l<0){
return -1;
}
int k;
for(int j=n-1;j>l;j--){
if(A[j]>A[l]){
k=j;
break;
}
}
swap(A[k],A[l]);
reverse(A.begin()+l+1,A.end());
stringstream geek(A);
long long int xx = 0;
geek >> xx;
if(xx>INT_MAX){
return -1;
}
return xx;
}
};