Skip to content

Instantly share code, notes, and snippets.

@pdu
Created February 20, 2013 14:09
Show Gist options
  • Select an option

  • Save pdu/4995771 to your computer and use it in GitHub Desktop.

Select an option

Save pdu/4995771 to your computer and use it in GitHub Desktop.
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory. Here are some examples. Inputs are in the left…
class Solution {
public:
void nextPermutation(vector<int> &num) {
if (!next_permutation(num.begin(), num.end()))
sort(num.begin(), num.end());
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment