Skip to content

Commit e88daad

Browse files
committed
add next permutation
1 parent f945e88 commit e88daad

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

next-permutation/README.md

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.

0 commit comments

Comments
 (0)