Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0254eef

Browse files
Create 46-Permutations.java
1 parent671dd5c commit0254eef

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎java/46-Permutations.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
importjava.util.Arrays;
2+
importjava.util.ArrayList;
3+
4+
classSolution {
5+
publicList<List<Integer>>permute(int[]nums) {
6+
List<List<Integer>>ans =newArrayList<>();
7+
function(ans,nums,0);
8+
returnans;
9+
}
10+
11+
publicvoidfunction(List<List<Integer>>ans,int[]arr,intstart) {
12+
if (start ==arr.length) {
13+
List<Integer>list =newArrayList();
14+
for (inti =0;i<arr.length;i++)
15+
list.add(arr[i]);
16+
ans.add(list);
17+
return;
18+
}
19+
20+
for (inti =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+
publicvoidswap(int[]arr,inta,intb) {
28+
inttemp =arr[a];
29+
arr[a] =arr[b];
30+
arr[b] =temp;
31+
}
32+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp