File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .ArrayList ;
3+
4+ class Solution {
5+ public List <List <Integer >> permute (int [] nums ) {
6+ List <List <Integer >> ans = new ArrayList <>();
7+ function (ans , nums , 0 );
8+ return ans ;
9+ }
10+
11+ public void function (List <List <Integer >> ans , int [] arr , int start ) {
12+ if (start == arr .length ) {
13+ List <Integer > list = new ArrayList ();
14+ for (int i =0 ; i <arr .length ; i ++)
15+ list .add (arr [i ]);
16+ ans .add (list );
17+ return ;
18+ }
19+
20+ for (int i = start ; i <arr .length ; i ++) {
21+ swap (arr , start , i );
22+ function (ans , arr , start +1 );
23+ swap (arr , start , i );
24+ }
25+ }
26+
27+ public void swap (int [] arr , int a , int b ) {
28+ int temp = arr [a ];
29+ arr [a ] = arr [b ];
30+ arr [b ] = temp ;
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments