Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkingtospace/eb503051e0861e18af6f to your computer and use it in GitHub Desktop.
Save walkingtospace/eb503051e0861e18af6f to your computer and use it in GitHub Desktop.
https://oj.leetcode.com/problems/permutations/
class Solution {
public:
vector<vector<int>> permute(vector<int> &num) {
vector<vector<int>> res;
if(num.size() == 0) {
return res;
}
permute(0, num.size(), num, res);
return res;
}
void permute(int i, int n, vector<int>& num, vector<vector<int>>& res) {
if(i >= n-1) {
res.push_back(num);
return;
}
for(int k=i; k<num.size() ; ++k) {
swap(num[i],num[k]);
permute(i+1,n,num,res);
swap(num[i], num[k]);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment