Skip to content

Instantly share code, notes, and snippets.

@adamkorg
Created January 10, 2020 23:31
Show Gist options
  • Save adamkorg/1d306abeb41eede8ff0dab43e1569d25 to your computer and use it in GitHub Desktop.
Save adamkorg/1d306abeb41eede8ff0dab43e1569d25 to your computer and use it in GitHub Desktop.
Leetcode 46: Permutations (STL next_permutation)
#include <iostream>
#include <vector>
using namespace std;
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
if (nums.size() == 0) return res;
sort(nums.begin(), nums.end());
res.push_back(nums);
while (next_permutation(nums.begin(), nums.end()))
res.push_back(nums);
return res;
}
int main() {
vector<int> nums {0,-1,1}; //{1,2,3};
auto res = permute(nums);
for (int i=0; i<res.size(); ++i) {
for (int j=0; j<res[i].size(); ++j) cout << res[i][j] << " ";
cout << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment