LeetCode(Permutations)
来源:互联网 发布:东软医保软件收费 编辑:程序博客网 时间:2024/05/17 06:43
Permutations
深搜:
class Solution {public: vector<vector<int> > permute(vector<int>& num) { sort(num.begin(),num.end()); vector<vector<int> > result; vector<int> path; dfs(num,result,path); return result; } void dfs(vector<int> &num,vector<vector<int> > &result,vector<int> &path) { if(path.size()==num.size()) { result.push_back(path); return; } for(int i=0;i<num.size();++i) { if(find(path.begin(),path.end(),num[i])==path.end()) { path.push_back(num[i]); dfs(num,result,path); path.pop_back(); } } }};
Permutations II
深搜:
class Solution {public: vector<vector<int> > permuteUnique(vector<int>& num) { sort(num.begin(),num.end()); vector<vector<int>> result; vector<int> path; unordered_map<int,int> map; for(int i=0;i<num.size();++i) { if(map.find(num[i])==map.end())map[num[i]]=1; else ++map[num[i]]; } dfs(num,result,path,map); return result; } void dfs(vector<int> &num,vector<vector<int> > &result,vector<int> &path,unordered_map<int,int> &map) { if(path.size()==num.size())result.push_back(path); for(unordered_map<int,int>::iterator i=map.begin();i!=map.end();++i) { int cnt=0; for(int j=0;j<path.size();++j) { if(path[j]==i->first)++cnt; } if(cnt<i->second) { path.push_back(i->first); dfs(num,result,path,map); path.pop_back(); } } }};
0 0
- 【LeetCode】Permutations && Permutations II
- leetcode: Permutations/Permutations II
- leetcode Permutations & Permutations II
- [Leetcode]Permutations && Permutations II
- LeetCode: Permutations
- LeetCode: Permutations
- [Leetcode] Permutations
- [LeetCode] Permutations
- Leetcode: Permutations
- [Leetcode] Permutations
- [Leetcode] Permutations
- [LeetCode]Permutations
- LeetCode-Permutations
- [leetcode] permutations
- LeetCode - Permutations
- Leetcode: Permutations
- 【leetcode】Permutations
- 【LeetCode】Permutations
- poj 2398 Toy Storage
- CSS3 HTML
- [leetcode] Min Stack
- Android4.2.2下Stagefright多媒体架构中的A31的OMX插件和Codec组件
- 监察用户对SharePoint做的操作
- LeetCode(Permutations)
- 在django中使用 mysql 数据库
- HDU_5224 Tom and paper
- openfire
- 简要分析unity3d中剪不断理还乱的yield
- MFC文档视图(三):命令消息路由
- android中设置AlertDialog的大小
- 位运算
- oracle 死锁处理