Question
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly the same digits existing in the integer n and is greater in value than n. If no such positive 32-bit integer exists, you need to return -1.
Example 1:
Example 2:
Explanation
思路是:从最后开始,每两个数对比,如果前一个数比后一个数小,停下,这点就是要变的地方。然后,把这个位置用后面比它大的数中最小的那个数字替代,互换位置。之后再把这个位置后面的所有数倒置一下。同LeetCode No.31
比如 12487653
先替换 12587643
再倒置后面的数字 12534678