leetcode103:permutations-ii
2024-09-29 22:34:29
题目描述
给出一组可能包含重复项的数字,返回该组数字的所有排列
例如;
[1,1,2]的排列如下:
[1,1,2],[1,2,1], [2,1,1].
[1,1,2],[1,2,1], [2,1,1].
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].
class Solution {
int array[19]={0};
void permutation(vector<vector<int>> &ans ,vector<int> &num,int k,int n){
if (k==n)
ans.push_back(num);
else
{
for (int i=0;i<19;i++){
if (array[i]>0)
{
array[i]--;
num[k]=i-9;
permutation(ans, num, k+1, n);
array[i]++;
}
}
}
}
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
for (int i=0;i<num.size();i++){
array[num[i]+9]++;
}
vector<vector<int>> ans;
permutation(ans, num, 0, num.size());
return ans;
}
};
int array[19]={0};
void permutation(vector<vector<int>> &ans ,vector<int> &num,int k,int n){
if (k==n)
ans.push_back(num);
else
{
for (int i=0;i<19;i++){
if (array[i]>0)
{
array[i]--;
num[k]=i-9;
permutation(ans, num, k+1, n);
array[i]++;
}
}
}
}
public:
vector<vector<int> > permuteUnique(vector<int> &num) {
for (int i=0;i<num.size();i++){
array[num[i]+9]++;
}
vector<vector<int>> ans;
permutation(ans, num, 0, num.size());
return ans;
}
};
最新文章
- asp.net配置web.config支持jQuery.Uploadify插件上传大文件
- jprofiler_监控远程linux服务器的tomcat进程(实践)
- 加载信息,先从数据库取出5条实现分页,鼠标向上滑动触发Ajax再加载5条,达到异步刷新,优化加载。。。
- Python开发【第十一篇】:JavaScript
- Python验证Url地址的正则表达式
- java读取文件批量插入记录
- 面试整理之DOM事件阶段
- Hark的数据结构与算法练习之简单选择排序
- centos安装memcache与telnet
- The architecture of LTE network.
- Object c中的alloc和init问题
- Android获取当前时间与星期几
- Ionic android 底部tabs
- 深入理解JAVA序列化
- eclipse安装及配置pydev
- C++的字符串格式化库
- C#导出EXCEL,并生成charts表
- WPF INotifyPropertyChanged
- Python学习:16.Python面对对象(三、反射,构造方法,静态字段,静态方法)
- FPGA基础知识,应用,ASIC、ASSP区别(四)