-
Notifications
You must be signed in to change notification settings - Fork 5
/
556. Next Greater Element III.java
61 lines (61 loc) · 1.71 KB
/
556. Next Greater Element III.java
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
class Solution {
private int dipIndex(char[] arr) {
int indx = arr.length - 2;
while(indx >= 0 && (arr[indx] >= arr[indx + 1])) {
indx--;
}
return indx;
}
private int ceilIndex(char[] arr, int indx) {
for(int i = arr.length - 1; i >= 0; i--) {
if(arr[i] > arr[indx]) {
return i;
}
}
return -1;
}
private void swap(char[] arr, int left, int right) {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
private void reverse(char[] arr, int indx) {
int left = indx;
int right = arr.length - 1;
while(left < right) {
swap(arr, left, right);
left++;
right--;
}
}
public String nextGreaterElement_(String str) {
if(str.length() == 1) return "-1";
char[] arr = str.toCharArray();
int dipIndx = dipIndex(arr);
if(dipIndx == -1) {
return "-1";
}
int ceilIndx = ceilIndex(arr, dipIndx);
swap(arr, dipIndx, ceilIndx);
reverse(arr, dipIndx + 1);
return String.valueOf(arr);
}
// for leetcode, above problem
public int nextGreaterElement(int n) {
String str = n + "";
String res_ = nextGreaterElement_(str);
long res = Long.parseLong(res_);
if(res <= Integer.MAX_VALUE) {
return (int)res;
} else {
return -1;
}
}
}