-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNextPermutation.java
42 lines (41 loc) · 1.03 KB
/
NextPermutation.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
import java.util.Arrays;
// https://leetcode.com/problems/next-permutation/description/
public class NextPermutation {
public static void main(String[] args) {
int arr[] = {1,2,3,5,6,4};
nextPermutation(arr);
}
public static void nextPermutation(int[] nums) {
int s = 0;
int found = 0;
for(int i=nums.length-1;i>0;i--)
{
if(nums[i]>nums[i-1])
{
s = i-1;
found=1;
break;
}
}
if(found==0)
{
Arrays.sort(nums);
System.out.println(Arrays.toString(nums));
return;
}
int pivot = 0;
for(int i=nums.length-1;i>0;i--)
{
if(nums[i]>nums[s])
{
pivot = i;
break;
}
}
int temp = nums[pivot];
nums[pivot] = nums[s];
nums[s] = temp;
Arrays.sort(nums, s+1, nums.length);
System.out.println(Arrays.toString(nums));
}
}