题目:

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],
[2,1,1]
]

  

题解:

Solution 1 (TLE)

class Solution {
public:
void dfs(vector<int> nums, vector<vector<int>>& vv, vector<int>& v, vector<int>& visited, int level) {
if(level >= nums.size()) {
if(find(vv.begin(), vv.end(), v) == vv.end())
vv.push_back(v);
return;
}
for(int i=; i<nums.size(); ++i) {
if(visited[i] != ) continue;
v.push_back(nums[i]);
visited[i] = ;
dfs(nums, vv, v, visited, level+);
v.pop_back();
visited[i] = ;
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> vv;
vector<int> v, visited(nums.size(),);
dfs(nums, vv, v, visited, );
return vv;
}
};

Solution 2 ()

class Solution {
public:
void dfs(vector<int> nums, vector<vector<int>>& vv, vector<int>& v, vector<int>& visited, int level) {
if(level >= nums.size()) {
vv.push_back(v);
return;
}
for(int i=; i<nums.size(); ++i) {
if(visited[i] != ) continue;
if(i> && nums[i] == nums[i-] && visited[i-] == ) continue;
v.push_back(nums[i]);
visited[i] = ;
dfs(nums, vv, v, visited, level+);
v.pop_back();
visited[i] = ;
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> vv;
vector<int> v, visited(nums.size(),);
sort(nums.begin(), nums.end());
dfs(nums, vv, v, visited, );
return vv;
}
};

Solution 3 ()

class Solution {
public:
void dfs(vector<int> nums, set<vector<int>>& sv, int level) {
if(level >= nums.size()) {
sv.insert(nums);
return;
}
for(int i=level; i<nums.size(); ++i) {
if(i != level && nums[i] == nums[level]) continue;
swap(nums[i], nums[level]);
dfs(nums, sv, level+);
swap(nums[i], nums[level]);
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
set<vector<int>> sv;
vector<int> v, visited(nums.size(),);
sort(nums.begin(), nums.end());
dfs(nums, sv, );
return vector<vector<int>> (sv.begin(), sv.end());
}
};

Solution 4 ()

class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> aux(nums.begin(), nums.end());
sort(aux.begin(), aux.end());
do {
ans.emplace_back(aux.begin(), aux.end());
} while(next_permutation(aux.begin(), aux.end()));
return ans;
}
};

【LeetCode】047. Permutations II的更多相关文章

  1. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  2. 【LeetCode】47. Permutations II

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  3. 【一天一道LeetCode】#47. Permutations II

    一天一道LeetCode系列 (一)题目 Given a collection of numbers that might contain duplicates, return all possibl ...

  4. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  5. 【LeetCode】Two Sum II - Input array is sorted

    [Description] Given an array of integers that is already sorted in ascending order, find two numbers ...

  6. 【LeetCode】基本计算器II

    [问题]实现一个基本的计算器来计算一个简单的字符串表达式的值.字符串表达式仅包含非负整数,+, - ,*,/ 四种运算符和空格  .整数除法仅保留整数部分. 输入: "3+2*2" ...

  7. 【LeetCode 】N皇后II

    [问题]n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法.给定一个整数 n,返回 n 皇后不同的解决方案的数量. 示例: ...

  8. 【LeetCode】跳跃游戏II

    [问题]给定一个非负整数数组,你最初位于数组的第一个位置.数组中的每个元素代表你在该位置可以跳跃的最大长度.你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [,,,,] 输出: ...

  9. 【LeetCode】52. N-Queens II(位运算)

    [题意] 输出N皇后问题的解法个数. [题解] 解法一:传统dfs回溯,模拟Q放置的位置即可,应该不难,虽然能通过,但是时间复杂度很高. 解法二:位运算大法好! 首先要明白这道题里两个核心的位运算 1 ...

随机推荐

  1. Visual Studio 2017 for Mac Preview

    Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...

  2. erlang的RSA签名与验签

    1.RSA介绍 RSA是目前最有影响力的公钥加密算法,该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对 其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥,即公钥,而 ...

  3. 用HttpClient模拟HTTP的GET和POST请求(转)

    本文转自:http://blog.csdn.net/xiazdong/article/details/7724349 一.HttpClient介绍   HttpClient是用来模拟HTTP请求的,其 ...

  4. 数据结构:最小生成树--Kruskal算法

    Kruskal算法 Kruskal算法 求解最小生成树的还有一种常见算法是Kruskal算法.它比Prim算法更直观.从直观上看,Kruskal算法的做法是:每次都从剩余边中选取权值最小的,当然,这条 ...

  5. Python 进程、线程、协程、锁机制,你知多少?

    1.python的多线程到底有没有用? 2. 为什么在python里推荐使用多进程而不是多线程 3.进程.线程.协程.各种锁 4.Python多进程编程

  6. 配置springMVC时出现的问题

    配置springMVC时出现的问题 项目结构如图:

  7. Hibernate的配置文件 Hibernate.cfg.xml与xxx.hbm.xml

    1.hibernate.cfg.xml配置如下: (数据库连接配置) <?xml version="1.0" encoding="UTF-8"?>& ...

  8. framemarker的使用

    1 什么是framemarker framemarker是网页模版和数据模型的结合体.装载网页的时候,framemarker自动从数据模型中提取数据并生成html页面. 2 framemarker怎么 ...

  9. wepy项目中使用async await

    https://github.com/Tencent/wepy/wiki/wepy项目中使用async-await

  10. keras:Exception: Error when checking model target

    问题: 用keras的functional API搭建多输入多输出模型时,报错某个输出层对应的类标数组大小与模型中不一致. 解决办法:升级keras到最新版(doge脸)keras迭代太快了啊摔,总有 ...