题解 | #没有重复项数字的所有排列#
没有重复项数字的所有排列
http://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
class Solution {
public:
void function(vector<int> &num, vector<int> &ans, vector<vector<int>> &res, vector<bool>& flag) {
if(ans.size() == num.size()) {
res.push_back(ans);
return;
}
for(int i = 0; i < num.size(); i++) {
if(!flag[i]) {
flag[i] = true;
ans.push_back(num[i]);
function(num, ans, res, flag);
ans.pop_back();
flag[i] = false;
}
}
}
vector<vector<int> > permute(vector<int> &num) {
vector<vector<int>> res;
int size = num.size();
if(size == 0)
return res;
sort(num.begin(), num.end());
vector<int> ans;
vector<bool> flag(num.size(), false);
function(num, ans, res, flag);
return res;
}
};