4 5 6 3 2 1
|
p
2) scan from right to left, find the first element that is greater than p.
4 5 6 3 2 1
|
q
3) swap p and q
4 5 6 3 2 1
swap
4 6 5 3 2 1
4) reverse elements [p+1, nums.length]
4 6 1 2 3 5