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

Commit99acaf0

Browse files
author
applewjg
committed
permutations
Change-Id: I4b4cac41ef886ca1bc53090fa500f433873d5262
1 parent2e1678b commit99acaf0

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

‎Permutations.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 25, 2014
4+
Problem: Permutations
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/permutations/
7+
Notes:
8+
Given a collection of numbers, return all possible permutations.
9+
For example,
10+
[1,2,3] have the following permutations:
11+
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
12+
13+
Solution: dfs...
14+
*/
15+
publicclassSolution {
16+
publicList<List<Integer>>permute_1(int[]num) {
17+
List<List<Integer>>res=newArrayList<List<Integer>>();
18+
List<Integer>path=newArrayList<Integer>();
19+
boolean[]free=newboolean[num.length];
20+
Arrays.fill(free, true);
21+
permuteRe(num,res,path,free);
22+
returnres;
23+
}
24+
voidpermuteRe(int[]num,List<List<Integer>>res,List<Integer>path,boolean[]free) {
25+
if(path.size()==num.length) {
26+
ArrayList<Integer>tmp=newArrayList<Integer>(path);
27+
res.add(tmp);
28+
return;
29+
}
30+
for (inti=0;i<num.length;++i) {
31+
if (free[i]== true) {
32+
free[i]= false;
33+
path.add(num[i]);
34+
permuteRe(num,res,path,free);
35+
path.remove(path.size()-1);
36+
free[i]= true;
37+
}
38+
}
39+
}
40+
publicbooleannextPermutation(int[]num) {
41+
intlast=num.length-1;
42+
inti=last;
43+
while (i>0&&num[i-1] >=num [i])--i;
44+
for (intl=i,r=last;l<r;++l,--r) {
45+
num[l]=num[l] ^num[r];
46+
num[r]=num[l] ^num[r];
47+
num[l]=num[l] ^num[r];
48+
}
49+
if (i==0) {
50+
return false;
51+
}
52+
intj=i;
53+
while (j <=last&&num[i-1] >=num[j])++j;
54+
num[i-1]=num[i-1] ^num[j];
55+
num[j]=num[i-1] ^num[j];
56+
num[i-1]=num[i-1] ^num[j];
57+
return true;
58+
}
59+
publicList<List<Integer>>permute_2(int[]num) {
60+
List<List<Integer>>res=newArrayList<List<Integer>>();
61+
Arrays.sort(num);
62+
do {
63+
List<Integer>path=newArrayList<Integer>();
64+
for (inti :num)path.add(i);
65+
res.add(path);
66+
}while(nextPermutation(num));
67+
returnres;
68+
}
69+
}

‎PermutationsII.h

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 25, 2014
4+
Problem: Permutations II
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/permutations-ii/
7+
Notes:
8+
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
9+
For example,
10+
[1,1,2] have the following unique permutations:
11+
[1,1,2], [1,2,1], and [2,1,1].
12+
13+
Solution: dfs...
14+
*/
15+
publicclassSolution {
16+
publicList<List<Integer>>permuteUnique_1(int[]num) {
17+
List<List<Integer>>res=newArrayList<List<Integer>>();
18+
List<Integer>path=newArrayList<Integer>();
19+
Arrays.sort(num);
20+
boolean[]visited=newboolean[num.length];
21+
Arrays.fill(visited, false);
22+
permuteRe(num,res,path,visited);
23+
returnres;
24+
}
25+
voidpermuteRe(int[]num,List<List<Integer>>res,List<Integer>path,boolean[]visited) {
26+
if(path.size()==num.length) {
27+
ArrayList<Integer>tmp=newArrayList<Integer>(path);
28+
res.add(tmp);
29+
return;
30+
}
31+
for (inti=0;i<num.length;++i) {
32+
if(visited[i]||(i!=0&&num[i-1]==num[i]&&visited[i-1]))continue;
33+
visited[i]= true;
34+
path.add(num[i]);
35+
permuteRe(num,res,path,visited);
36+
path.remove(path.size()-1);
37+
visited[i]= false;
38+
}
39+
}
40+
publicbooleannextPermutation(int[]num) {
41+
intlast=num.length-1;
42+
inti=last;
43+
while (i>0&&num[i-1] >=num [i])--i;
44+
for (intl=i,r=last;l<r;++l,--r) {
45+
num[l]=num[l] ^num[r];
46+
num[r]=num[l] ^num[r];
47+
num[l]=num[l] ^num[r];
48+
}
49+
if (i==0) {
50+
return false;
51+
}
52+
intj=i;
53+
while (j <=last&&num[i-1] >=num[j])++j;
54+
num[i-1]=num[i-1] ^num[j];
55+
num[j]=num[i-1] ^num[j];
56+
num[i-1]=num[i-1] ^num[j];
57+
return true;
58+
}
59+
publicList<List<Integer>>permuteUnique_2(int[]num) {
60+
List<List<Integer>>res=newArrayList<List<Integer>>();
61+
Arrays.sort(num);
62+
do {
63+
List<Integer>path=newArrayList<Integer>();
64+
for (inti :num)path.add(i);
65+
res.add(path);
66+
}while(nextPermutation(num));
67+
returnres;
68+
}
69+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp