

class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i = nums.size() - 2, j = nums.size() - 1;
while(nums[i] >= nums[i+1]) i--;
if(i >= 0){
while(nums[j] <= nums[i]) j--;//这里一定要是小于等于,因为等于也不可以交换,否则就不是下一个排列组合了
swap(nums[j], nums[i]);
}
reverse(nums.begin() + i + 1, nums.end());
}
};

http://bangbingsyb.blogspot.com/2014/11/leetcode-next-permutation.html
网友评论