代码实现:
思路:两遍扫描
void swap(int *a, int *b) {int t = *a;*a = *b;*b = t; }void reverse(int *nums, int l, int r) {while (l < r) {swap(nums + l, nums + r);l++;r--;} }void nextPermutation(int *nums, int numsSize) {int i = numsSize - 2;while (i >= 0 && nums[i] >= nums[i + 1]) {i--;}if (i >= 0) {int j = numsSize - 1;while (j >= 0 && nums[i] >= nums[j]) {j--;}swap(nums + i, nums + j);}reverse(nums, i + 1, numsSize - 1); }