Skip to content

Commit 1a872bf

Browse files
committed
commit changes to gh-pages aug 31
1 parent b5701fe commit 1a872bf

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
## [Generation in lexicographic order](http://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order)
12

2-
## TODO
3-
* write down thinking
3+
I can not work out this without Google's help.
44

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.
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1+
## Variant of [Next Permutation](../next-permutation)
12

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.
44

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+
```

_includes/_root/permutations-ii/Solution.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
public class Solution {
2+
23
void swap(int x[], int a, int b) {
34
int t = x[a];
45
x[a] = x[b];
@@ -37,19 +38,18 @@ public boolean nextPermutation(int[] num) {
3738
return true;
3839
}
3940

40-
ArrayList<Integer> asList(int[] num){
41+
List<Integer> asList(int[] num){
4142
ArrayList<Integer> l = new ArrayList<Integer>(num.length);
4243
for(int i = 0; i < num.length; i++)
4344
l.add(num[i]);
4445

4546
return l;
4647
}
4748

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) {
5050
Arrays.sort(num);
5151

52-
ArrayList<ArrayList<Integer>> found = new ArrayList<ArrayList<Integer>>();
52+
ArrayList<List<Integer>> found = new ArrayList<List<Integer>>();
5353
found.add(asList(num));
5454

5555
while(nextPermutation(num)){
@@ -58,4 +58,4 @@ public ArrayList<ArrayList<Integer>> permuteUnique(int[] num) {
5858

5959
return found;
6060
}
61-
}
61+
}

next-permutation/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: solution
33
title: Next Permutation
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-31 00:42:19 +0800
55
---
66
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
77
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

permutations-ii/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
layout: solution
33
title: Permutations II
4-
date: 2014-07-23 02:42:48 +0800
4+
date: 2014-08-31 00:52:32 +0800
55
---
66
{% assign leetcode_name = {{page.path | remove: '/index.md'}} %}
77
{% assign leetcode_readme = {{leetcode_name | append: '/README.md' | prepend: '_root/' }} %}

0 commit comments

Comments
 (0)