Skip to content

Instantly share code, notes, and snippets.

@zhangxiaomu01
Created December 9, 2018 03:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangxiaomu01/1e7353d1fbb425215ea2e353956b7e3e to your computer and use it in GitHub Desktop.
Save zhangxiaomu01/1e7353d1fbb425215ea2e353956b7e3e to your computer and use it in GitHub Desktop.
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i = nums.size() - 2;
while(i >= 0 && nums[i+1] <= nums[i])
i--;
if(i>=0){
int j = nums.size() - 1;
while(j >= i+1 && nums[j] <= nums[i])
j--;
swap(nums[i],nums[j]);
}
reverse(nums.begin()+i+1, nums.end());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment