Permutations Solutions in C++
Number 46
Difficulty Medium
Acceptance 63.7%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/permutations/// Author : Hao Chen// Date : 2014-06-21#include <stdio.h>#include <stdlib.h>#include <iostream>#include <vector>using namespace std;/*{ 1 2 3 }{ 2 1 3 }{ 3 2 1 }{ 1 3 2 }{ 2 3 1 }{ 3 1 2 }*//** The algroithm - Take each element in array to the first place.** For example:** 0) initalization** pos = 0* [1, 2, 3]** 1) take each element into the first place,** pos = 1* [1, 2, 3] ==> [2, 1, 3] , [3, 1, 2]** then we have total 3 answers* [1, 2, 3], [2, 1, 3] , [3, 1, 2]** 2) take each element into the "first place" -- pos** pos = 2* [1, 2, 3] ==> [1, 3, 2]* [2, 1, 3] ==> [2, 3, 1]* [3, 1, 2] ==> [3, 2, 1]** then we have total 6 answers* [1, 2, 3], [2, 1, 3] , [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]** 3) pos = 3 which greater than length of array, return.**/vector<vector<int> > permute(vector<int> &num) {vector<vector<int> > vv;vv.push_back(num);if (num.size() <2){return vv;}int pos=0;while(pos<num.size()-1){int size = vv.size();for(int i=0; i<size; i++){//take each number to the first placefor (int j=pos+1; j<vv[i].size(); j++) {vector<int> v = vv[i];int t = v[j];v[j] = v[pos];v[pos] = t;vv.push_back(v);}}pos++;}return vv;}int main(int argc, char** argv){int n = 3;if (argc>1){n = atoi(argv[1]);}vector<int> v;for (int i=0; i<n; i++) {v.push_back(i+1);}vector<vector<int> > vv;vv = permute(v);for(int i=0; i<vv.size(); i++) {cout << "{ ";for(int j=0; j<vv[i].size(); j++){cout << vv[i][j] << " ";}cout << "}" <<endl;}return 0;}
C++ solution by pezy/LeetCode
#include <algorithm>#include <vector>using std::vector; using std::next_permutation;class Solution {public:vector<vector<int> > permute(vector<int> &num) {vector<vector<int> > ret;sort(num.begin(), num.end());do {ret.push_back(num);} while (next_permutation(num.begin(), num.end()));return ret;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/permutations/description//// Author : liuyubobobo/// Time : 2017-11-18#include <iostream>#include <vector>using namespace std;/// Most Naive Recursive/// Time Complexity: O(n^n)/// Space Complexity: O(n)class Solution {private:vector<vector<int>> res;vector<bool> used;void generatePermutation(const vector<int>& nums, int index, vector<int>& p){if(index == nums.size()){res.push_back(p);return;}for(int i = 0 ; i < nums.size() ; i ++)if(!used[i]){used[i] = true;p.push_back(nums[i]);generatePermutation(nums, index + 1, p );p.pop_back();used[i] = false;}return;}public:vector<vector<int>> permute(vector<int>& nums) {res.clear();if(nums.size() == 0)return res;used = vector<bool>(nums.size(), false);vector<int> p;generatePermutation(nums, 0, p);return res;}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {int nums[] = {1, 2, 3};vector<int> vec(nums, nums + sizeof(nums)/sizeof(int) );vector<vector<int>> res = Solution().permute(vec);for(int i = 0 ; i < res.size() ; i ++)printVec(res[i]);return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/permutations/description//// Author : liuyubobobo/// Time : 2017-12-17#include <iostream>#include <vector>using namespace std;/// Recursive get all the permutations in place/// Time Complexity: O(n!)/// Space Complexity: O(n)class Solution {private:vector<vector<int>> res;void generatePermutation(vector<int>& nums, int index){if(index == nums.size()){res.push_back(nums);return;}for(int i = index ; i < nums.size() ; i ++){swap(nums[i], nums[index]);generatePermutation(nums, index + 1);swap(nums[i], nums[index]);}return;}public:vector<vector<int>> permute(vector<int>& nums) {res.clear();if(nums.size() == 0)return res;generatePermutation(nums, 0);return res;}};void printVec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {vector<int> vec = {1, 2, 3};vector<vector<int>> res = Solution().permute(vec);for(int i = 0 ; i < res.size() ; i ++)printVec(res[i]);return 0;}