1
1
import java .io .*;
2
2
import java .util .*;
3
+
3
4
//https://leetcode.com/problems/permutation-sequence/
4
5
public class PermutationSequence {
5
6
static BufferedReader br ;
6
7
static StringTokenizer st ;
7
8
PermutationSequence ps = new PermutationSequence ();
8
9
9
- static int nextInt ()throws IOException
10
- {
10
+ static int nextInt () throws IOException {
11
11
return Integer .parseInt (next ());
12
12
}
13
13
14
- static String next ()throws IOException
15
- {
16
- while (st == null || !st .hasMoreTokens ())
17
- {
18
- st =new StringTokenizer (br .readLine ());
14
+ static String next () throws IOException {
15
+ while (st == null || !st .hasMoreTokens ()) {
16
+ st = new StringTokenizer (br .readLine ());
19
17
}
20
18
return st .nextToken ();
21
19
}
22
20
23
- public static void main (String [] args )throws IOException
24
- {
21
+ public static void main (String [] args ) throws IOException {
25
22
br = new BufferedReader (new InputStreamReader (System .in ));
26
-
23
+
27
24
int n = nextInt ();
28
25
int k = nextInt ();
29
- System .out .println (getPermutation (n , k ));
26
+ System .out .println (getPermutation (n , k ));
30
27
}
31
28
32
29
public static String getPermutation (int n , int k ) {
33
30
List <Integer > nums = new ArrayList <>();
34
- for (int i = 0 ;i <n ;i ++)
35
- {
36
- nums .add (i +1 );
31
+ for (int i = 0 ; i < n ; i ++) {
32
+ nums .add (i + 1 );
37
33
}
38
34
List <List <Integer >> ans = new ArrayList <>();
39
- permute (nums ,ans ,0 ,n -1 );
40
-
41
-
42
- Collections .sort (ans ,new Comparator <List <Integer >>(){
43
-
35
+ permute (nums , ans , 0 , n - 1 );
36
+
37
+ Collections .sort (ans , new Comparator <List <Integer >>() {
38
+
44
39
@ Override
45
- public int compare (List <Integer > val1 , List <Integer > val2 )
46
- {
47
- if (val1 .get (0 )>val2 .get (0 ))
48
- {
40
+ public int compare (List <Integer > val1 , List <Integer > val2 ) {
41
+ if (val1 .get (0 ) > val2 .get (0 )) {
49
42
return 1 ;
50
- }
51
- else
52
- {
43
+ } else {
53
44
return -1 ;
54
45
}
55
46
}
56
47
});
57
-
58
-
59
-
48
+
60
49
System .out .println (ans );
61
50
StringBuilder sb = new StringBuilder ();
62
- nums = ans .get (k - 1 );
51
+ nums = ans .get (k - 1 );
63
52
int i = 0 ;
64
- while (i < nums .size () - 1 )
65
- {
53
+ while (i < nums .size () - 1 ) {
66
54
sb .append (nums .get (i ));
67
-
55
+
68
56
i ++;
69
57
}
70
58
sb .append (nums .get (i ));
71
-
59
+
72
60
String res = sb .toString ();
73
- return res ;
74
-
75
-
61
+ return res ;
62
+
76
63
}
77
-
78
- static void permute (List <Integer > nums ,List <List <Integer >> ans , int l , int r )
79
- {
80
- if (l == r )
81
- ans .add (new ArrayList <>(nums ));
82
- else
83
- {
84
- for (int i = l ; i <= r ; i ++)
85
- {
86
- nums = swap (nums ,l ,i );
87
- permute (nums ,ans , l +1 , r );
88
- nums = swap (nums ,l ,i );
89
- }
90
- }
64
+
65
+ static void permute (List <Integer > nums , List <List <Integer >> ans , int l , int r ) {
66
+ if (l == r )
67
+ ans .add (new ArrayList <>(nums ));
68
+ else {
69
+
70
+ for (int i = l ; i <= r ; i ++) {
71
+ nums = swap (nums , l , i );
72
+ permute (nums , ans , l + 1 , r );
73
+ nums = swap (nums , l , i );
74
+ }
75
+ }
91
76
}
92
-
93
- static List <Integer > swap (List <Integer > nums , int l ,int i )
94
- {
77
+
78
+ static List <Integer > swap (List <Integer > nums , int l , int i ) {
95
79
int temp ;
96
- temp = nums .get (i );
97
- nums .set (i ,nums .get (l ));
98
- nums .set (l ,temp );
80
+ temp = nums .get (i );
81
+ nums .set (i ,nums .get (l ));
82
+ nums .set (l ,temp );
99
83
return nums ;
100
84
}
101
-
102
-
103
-
104
-
85
+
105
86
}
0 commit comments