首页 > 代码库 > Permutations II
Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1].
vector<vector<int> >& permute(vector<int> vecInt){ vector<int> vecWorker; vector<vector<int> > vecPermu; vector<int> vecVisit(vecInt.size()); sort(vecInt.begin(), vecInt.end()); helper(vecPermu, vecWorker, vecInt, vecVisit); return vecPermu;}void helper(vector<vector<int> >& vecPermu, vector<int>& vecWorker, const vector<int>& vecInt, vector<int>& vecVisit){ if (vecWorker.size() == vecInt.size()) { vecPermu.push_back(vecWorker); return; } for (int i = 0; i < vecInt.size(); i++) { if (vecVisit[i] == 1 || (i != 0 && vecInt[i] == vecInt[i-1] && vecVisit[i-1] == 0)) { continue; } vecVisit[i] = 1; vecWorker.push_back(vecInt[i]; helper(vecPermu, vecWorker, vecInt); vecWorker.pop_back(vecInt[i]); vecVisit[i] = 0; }}
Permutations II
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。