Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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