Leetcode 46: Permutations (STL next_permutation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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