-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path556.next-greater-element-iii.cpp
64 lines (62 loc) · 1.28 KB
/
556.next-greater-element-iii.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @lc app=leetcode id=556 lang=cpp
*
* [556] Next Greater Element III
*
* https://leetcode.com/problems/next-greater-element-iii/description/
*
* algorithms
* Medium (30.43%)
* Total Accepted: 29.4K
* Total Submissions: 96.6K
* Testcase Example: '12'
*
* Given a positive 32-bit integer n, you need to find the smallest 32-bit
* integer which has exactly the same digits existing in the integer n and is
* greater in value than n. If no such positive 32-bit integer exists, you need
* to return -1.
*
* Example 1:
*
*
* Input: 12
* Output: 21
*
*
*
*
* Example 2:
*
*
* Input: 21
* Output: -1
*
*
*
*
*/
class Solution {
public:
int nextGreaterElement(int n) {
vector<int> vec;
while(n){
vec.push_back(n%10);
n/=10;
}
int i = 0;
long ans = 0l;
while(i+1<vec.size() && vec[i] <= vec[i+1])
i++;
if(i+1 >= vec.size())
return -1;
int j = 0;
while(j<i+1 && vec[j] <= vec[i+1])
j++;
swap(vec[j], vec[i+1]);
sort(vec.begin(), vec.begin()+i+1);
reverse(vec.begin(), vec.begin()+i+1);
for(int i = (int)vec.size()-1; i>=0; i--)
ans = ans*10l + (long)vec[i];
return ans<=INT_MAX?ans:-1;
}
};