题解 | #有重复项数字的全排列#
有重复项数字的全排列
https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
class Solution { public: vector<vector<int> > result; vector<vector<int> > permuteUnique(vector<int> &num) { if (num.size() == 0) return result; sort(num.begin(), num.end()); vector<bool> state(num.size(), false); vector<int> tmp; back_tracking(num, tmp, state); return result; } void back_tracking(vector<int>& num, vector<int>& tmp, vector<bool>& state) { if (tmp.size() == num.size()) { result.push_back(tmp); return; } for (int i = 0; i < num.size(); ++i) { if (state[i] || (i > 0 && num[i] == num[i-1] && state[i-1])) { continue; } state[i] = true; tmp.push_back(num[i]); back_tracking(num, tmp, state); state[i] = false; tmp.pop_back(); } return; } };
2023-剑指-回溯 文章被收录于专栏
2023-剑指-回溯