-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode556.java
More file actions
55 lines (52 loc) · 1.53 KB
/
Copy pathleetcode556.java
File metadata and controls
55 lines (52 loc) · 1.53 KB
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
package leetcode;
public class leetcode556 {
public static void main(String[] args) {
System.out.println(
new leetcode556().nextGreaterElement(1999999999)
);
}
public int nextGreaterElement(int n) {
char [] len =new String(""+n).toCharArray();
int [] aima=new int[len.length];
for (int i = 0; i <len.length ; i++) {
aima[i]=len[i]-'0';
}
if (nextPermutation(aima)){
int result=0;
for (int i = 0; i <aima.length ; i++) {
result+=aima[i]*Math.pow(10,aima.length-i-1);
if (result==-2147483648) return -1;
}
return result> Integer.MAX_VALUE?-1:result;
}else {
return -1;
}
}
public boolean nextPermutation(int[] nums) {
if (nums.length==0) return false;
int len=nums.length;
int i=len-1;
while(i>0&&nums[i-1]>=nums[i]){ //找到尾部的逆序序列
i--;
}
//旋转逆序数组
for (int j = i; j <=(i+len-1)/2 ; j++) {
int temp=nums[j];
nums[j]=nums[(i+len-1)-j];
nums[(i+len-1)-j]=temp;
}
if (i>0){
for (int j = i; j <len ; j++) {
if(nums[j]>nums[i-1]){
int temp=nums[i-1];
nums[i-1]=nums[j];
nums[j]=temp;
break;
}
}
}else{
return false;
}
return true;
}
}