File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Nextpermutation {
2
+ public void Next_permutation (int nums []){
3
+ int i =nums .length -2 ;
4
+ while (i >=0 && nums [i ]>=nums [i +1 ]){
5
+ i --;
6
+ }
7
+ if (i >=0 ){
8
+ int j =nums .length -1 ;
9
+ while (j >i && nums [j ]<=nums [i ]){
10
+ j --;
11
+ }
12
+ swap (nums ,i ,j );
13
+ }
14
+ reverse (nums ,i +1 );
15
+ }
16
+ public void swap (int []nums ,int i ,int j ){
17
+ int temp =nums [i ];
18
+ nums [i ]=nums [i +1 ];
19
+ nums [i +1 ]=temp ;
20
+ }
21
+ public void reverse (int []nums ,int start ){
22
+ int end =nums .length -1 ;
23
+ while (start <end ){
24
+ swap (nums ,start ,end );
25
+ start ++;
26
+ end --;
27
+ }
28
+ }
29
+ public static void main (String []args ){
30
+ int nums []={1 ,2 ,3 };
31
+ Nextpermutation next =new Nextpermutation ();
32
+ next .Next_permutation (nums );
33
+ // System.out.println(next.Next_permutation(nums[1,2,3]));
34
+ for (int x :nums ){
35
+ System .out .println (x +" " );
36
+ }
37
+ }
38
+ }
39
+
You can’t perform that action at this time.
0 commit comments