-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode31.java
More file actions
44 lines (42 loc) · 1.18 KB
/
leetcode31.java
File metadata and controls
44 lines (42 loc) · 1.18 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
import java.util.Arrays;
public class leetcode31 {
public static void nextPermutation(int[] nums) {
int index = -1;
for(int i = nums.length-2 ; i>=0; i--){
if(nums[i]<nums[i+1]){
index = i;
break;
}
}
if(index == -1){
for(int i = 0; i <= (nums.length/2)-1; i++){
int temp = nums[i];
nums[i] = nums[nums.length-1-i];
nums[nums.length-1-i] = temp;
}
return;
}
for(int j = nums.length-1; j>index;j--){
if(nums[j]>nums[index]){
int temp = nums[j];
nums[j] = nums[index];
nums[index] = temp;
break;
}
}
int left = index+1;
int right = nums.length-1;
while(left<right){
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
right--;
}
}
public static void main(String[] args) {
int[] nums = {5,4,3,2,1};
nextPermutation(nums);
System.out.println(Arrays.toString(nums));
}
}