File tree Expand file tree Collapse file tree 5 files changed +40
-11
lines changed Expand file tree Collapse file tree 5 files changed +40
-11
lines changed Original file line number Diff line number Diff line change
1
+ ## [ Generation in lexicographic order] ( http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order )
1
2
2
- ## TODO
3
- * write down thinking
3
+ I can not work out this without Google's help.
4
4
5
+ steps below are copied from Wikipedia:
6
+
7
+ 1 . Find the largest index k such that a[ k] < a[ k + 1] . If no such index exists, the permutation is the last permutation.
8
+ 1 . Find the largest index l greater than k such that a[ k] < a[ l] .
9
+ 1 . Swap the value of a[ k] with that of a[ l] .
10
+ 1 . Reverse the sequence from a[ k + 1] up to and including the final element a[ n] .
11
+
12
+
13
+ [ Fisherlei's Image] ( http://fisherlei.blogspot.com/2012/12/leetcode-next-permutation.html ) is a good illustration.
14
+ You can see an image that how this solution goes.
Original file line number Diff line number Diff line change
1
+ ## Variant of [ Next Permutation] ( ../next-permutation )
1
2
2
- ## TODO
3
- * write down thinking
3
+ With the help of [ Next Permutation] ( ../next-permutation ) , you can find all unique permutations from the first permutation.
4
4
5
+ ### First and Last
6
+
7
+ * First permutation is the numbers in non-descending order
8
+ * Last permutation is the numbers in non-ascending order
9
+
10
+
11
+ so the code is like
12
+
13
+ ```
14
+ sort(num) // make it the first
15
+
16
+
17
+ while num is not the last
18
+ call nextPermutation(num)
19
+
20
+ copy num into result
21
+
22
+
23
+ ```
Original file line number Diff line number Diff line change 1
1
public class Solution {
2
+
2
3
void swap (int x [], int a , int b ) {
3
4
int t = x [a ];
4
5
x [a ] = x [b ];
@@ -37,19 +38,18 @@ public boolean nextPermutation(int[] num) {
37
38
return true ;
38
39
}
39
40
40
- ArrayList <Integer > asList (int [] num ){
41
+ List <Integer > asList (int [] num ){
41
42
ArrayList <Integer > l = new ArrayList <Integer >(num .length );
42
43
for (int i = 0 ; i < num .length ; i ++)
43
44
l .add (num [i ]);
44
45
45
46
return l ;
46
47
}
47
48
48
- public ArrayList <ArrayList <Integer >> permuteUnique (int [] num ) {
49
- // Note: The Solution object is instantiated only once and is reused by each test case.
49
+ public List <List <Integer >> permuteUnique (int [] num ) {
50
50
Arrays .sort (num );
51
51
52
- ArrayList <ArrayList <Integer >> found = new ArrayList <ArrayList <Integer >>();
52
+ ArrayList <List <Integer >> found = new ArrayList <List <Integer >>();
53
53
found .add (asList (num ));
54
54
55
55
while (nextPermutation (num )){
@@ -58,4 +58,4 @@ public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
58
58
59
59
return found ;
60
60
}
61
- }
61
+ }
Original file line number Diff line number Diff line change 1
1
---
2
2
layout : solution
3
3
title : Next Permutation
4
- date : 2014-07-23 02 :42:48 +0800
4
+ date : 2014-08-31 00 :42:19 +0800
5
5
---
6
6
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
7
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
Original file line number Diff line number Diff line change 1
1
---
2
2
layout : solution
3
3
title : Permutations II
4
- date : 2014-07-23 02:42:48 +0800
4
+ date : 2014-08-31 00:52:32 +0800
5
5
---
6
6
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
7
7
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_ root/' }} %}
You can’t perform that action at this time.
0 commit comments