首页 > 其他分享 >lintcode: Permutations II

lintcode: Permutations II

时间:2022-12-01 19:04:19浏览次数:35  
标签:return sub nums int res Permutations lintcode II vector


Given a list of numbers with duplicate number in it. Find all unique permutations.

可以见我的博文​​全排列实现​​

class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of unique permutations.
*/
vector<vector<int> >res;

void dfs(vector<int> &nums,vector<int> &sub,int cur){
if(cur==nums.size()){
res.push_back(sub);
return;
}

for(int i=0;i<nums.size();i++){
if(i==0||nums[i]!=nums[i-1]){
int c1=0,c2=0;
for(int j=0;j<nums.size();j++){
if(nums[j]==nums[i]){
c1++;
}
}

for(int j=0;j<cur;j++){
if(sub[j]==nums[i]){
c2++;
}
}

if(c2<c1){
sub[cur]=nums[i];
dfs(nums,sub,cur+1);
}

}
}
}

vector<vector<int> > permuteUnique(vector<int> &nums) {
// write your code here

int len=nums.size();

if(len==0){
return res;
}
sort(nums.begin(),nums.end());
vector<int> sub(len);

dfs(nums,sub,0);

return res;
}
};


标签:return,sub,nums,int,res,Permutations,lintcode,II,vector
From: https://blog.51cto.com/u_15899184/5903613

相关文章

  • lintcode: Subsets II
    Givenalistofnumbersthatmayhasduplicatenumbers,returnallpossiblesubsets1.先排序;再按求Subsets一样的做法,只是添加前检查是否已经存在。耗时171mscla......
  • lintcode:Subarray Sum Closest
    Givenanintegerarray,findasubarraywithsumclosesttozero.Returntheindexesofthefirstnumberandlastnumber.ExampleGiven[-3,1,1,-3,5],retur......
  • lintcode: Sqrt(x)
    Implementintsqrt(intx).Computeandreturnthesquarerootofx.Examplesqrt(3)=1sqrt(4)=2sqrt(5)=2sqrt(10)=3ChallengeO(log(x))求非线性方程的解可以......
  • 隧道调试本地IIS Express报400错误,127.0.0.1无法访问
    我们在使用​​https://natapp.cn​​​时,难免会本地进行调试代码,自己要申请一个隧道域名,比如:​​http://ls.nat600.top​​,然后在访问的时候加上端口会报错,使用localhost加......
  • .net core 的IIS设置环境变量 ASPNETCORE_ENVIRONMENT
    IIS统一设置ASPNETCORE_ENVIRONMENT的变量,不需要每个站点都在webconfig里进行配置,这样每次发布版本可能会被覆盖,比较麻烦,所以统一更是最好的选择,那具体步骤呢?步骤如下:1、打......
  • IIS put请求 报HTTP Error 405 - Method Not Allowed
    在新的服务器上部署了一个.netcore的项目,部分请求地址使用了put、delete方式,导致无法正常请求,报Error405-MethodNotAllowed。由于配置IIS时把“WebDAV发布”给勾选了......
  • 力扣275(jav&python)-H 指数 II(中等)
    题目:给你一个整数数组citations,其中citations[i]表示研究者的第i篇论文被引用的次数,citations已经按照 升序排列 。计算并返回该研究者的h 指数。h指数的定......
  • SAP MM 使用两个STO实现免关税跨国公司间转储(II)
    SAPMM使用两个STO实现免关税跨国公司间转储(II)  在某个项目里,使用2个STO实现免关税的跨国公司间转储流程里,第一个STO是手工创建的,但是第二个STO是第一个STO创建后通......
  • IIS 网站搭建,内部可以访问,外部不能访问
    文章来源:https://blog.csdn.net/pigeon79/article/details/123616787今天配置好IIS环境的时候发现外网不能访问了,内网可以访问,检查了绑定、权限等等都没问题,到底是哪里出......
  • IIS报错:未能加载文件或程序集MySql.ConnectorInstaller, Version=8.0.8.0, Culture=ne
    删除machine.config中相关配置<siteMap><providers><addname="MySqlSiteMapProvider"type="MySql.Web.SiteMap.MySqlSiteMapProvider,MySql.ConnectorInstal......