-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
executable file
·47 lines (39 loc) · 1.07 KB
/
Permutations.cpp
File metadata and controls
executable file
·47 lines (39 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// Permutations.cpp
// leetcode
//
// Created by witwolf on 5/13/15.
// Copyright (c) 2015 witwolf. All rights reserved.
//
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
vector<vector<int> > permute(vector<int>& nums) {
vector<vector<int> > result;
permute(nums,0, (int) nums.size(),result);
return result;
}
private:
void permute(vector<int>& nums,int i,int n,vector<vector<int> >& result){
if(i == n-1){
result.push_back(vector<int>(nums));
}
for(int j = i;j < n;++j){
swap(nums[i], nums[j]);
permute(nums,i+1,n,result);
swap(nums[i], nums[j]);
}
}
};
int main(int argc,char **argv){
Solution s;
int nums[] = {1,2,2,2};
vector<int> a = vector<int>(nums,nums+sizeof(nums)/sizeof(int));
vector<vector<int> > result = s.permute(a);
for(auto it = result.begin() ; it != result.end(); ++it){
copy(it->begin(), it->end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
}