1. Next Permutation

1. Next Permutation

swap + reverse the remaining string

158476521

除非所有数字都是descending order,要不然就肯定有next permutation。由此,可以得到灵感,从end开始,一直往前走,如果到现在位置都是descending的话,那么继续,直到走到一个数字大于它后边的数字。

158(4)76521

4和next great number交换(从尾巴开始,第一个比4大的数字)

158(5)76(4)21

因为走到4的时候,经过的数字序列都是descending的,所以即使把5和4调换了,也能保证当前位置后面的数字都是descending order。

next great permutation肯定是小的数字要在前面,形成ascending order才是最小的数字组合。在已经知道当前数字后面的数字组合是ascending order的情况下,只要reverse后面的部分就可以了。

158(576421) reverse括号里面的部分。

results matching ""

    No results matching ""