Permutation
Next Permutation
Given a permutation, get the next permutation.
Example:
given
[2,1,3]
, the next permutation is[2,3,1]
.given
[1,2,1]
, the next permutation is[2,1,1]
.
The first section
is looking for the last element in front of the descending subarray.
For example, nums = [2,1,4,3]
, then i
will point to 1
which is the last element in front of the descending subarray [4, 3]
.
If not found, it means that the whole array is descending. The next permutation will be the reverse of the input array.
If found, we can swap it with the smallest element greater than itself, which is covered in the if
condition.
And to get the next permutation, we need to reverse the subarray starting from nums[i + 1]
to the end.
For example, nums = [1,6,3,5,4,2]
, then i = 2
pointing to 3
and j = 4
pointing to 4
. We swap nums[i]
and nums[j]
so swapping them gives [1,6,4,5,3,2]
, then reverse the subarray starting from 2
to the end, getting [1,6,4,2,3,5]
.
Permutations (distinct digits)
See my notes here
Permutations (might contain duplicate)
See my notes here
Problems
Last updated