(待解决,效率低下)47. Permutations II C++回溯法
思路是在相似题Permutations的基础上,将结果放到set中,利用set容器不会出现重复元素的特性,得到所需结果
但是利用代码中的/* */部分通过迭代器遍历set将set中的元素放在一个新的vector中时,会出现memory limit exceeded错误(原因??)
上网查找后发现可以直接通过return vector<vector<int>> (mySet.begin(),mySet.end())得到结果,并且代码通过。
class Solution {
public:
void backTrack(vector<int> nums, set<vector<int>>& mySet, vector<int> res, int k, int m[])
{
if(k == nums.size())
{
mySet.insert(res);
}
else
{
for(int i=;i<nums.size();i++)
{
if(!m[i])
{
m[i] = ;
res.push_back(nums.at(i));
backTrack(nums,mySet,res,k+,m);
res.pop_back();
m[i] = ;
}
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> res;
set<vector<int>> mySet;
int m[nums.size()] = {,};//true为未用过
backTrack(nums,mySet,res,,m);
/*set<vector<int>>::iterator itr = mySet.begin();
while(itr != mySet.end())
{
ans.push_back(*itr);
for(int i=0; i<(*itr).size();i++)
{
cout << (*itr).at(i) << ' ';
}
cout << endl;
itr++;
}*/
return vector<vector<int>> (mySet.begin(),mySet.end());
}
};
(待解决,效率低下)47. Permutations II C++回溯法的更多相关文章
- (效率低下)77. Combinations C++回溯法 组合
https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...
- [Leetcode][Python]47: Permutations II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 47: Permutations IIhttps://oj.leetcode. ...
- 34,Leetcode 组合总和I,II -C++ 回溯法
I 题目描述 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的数字可以无 ...
- leetCode 47.Permutations II (排列组合II) 解题思路和方法
Permutations II Given a collection of numbers that might contain duplicates, return all possible un ...
- leetcode46. Permutations 、47. Permutations II、 剑指offer字符串的排列
字符串排列和PermutationsII差不多 Permutations第一种解法: 这种方法从0开始遍历,通过visited来存储是否被访问到,level代表每次已经存储了多少个数字 class S ...
- 【LeetCode】47. Permutations II
Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- [LeetCode] 47. Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 47. Permutations II(medium, backtrack, 重要, 条件较难思考)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
随机推荐
- Kylin——CDH
CDH:Cloudera‘s Distribution,including Apache Hadoop. Hadoop众多分支中的一种,可直接用于成产环境 CM:Cloudera Manager
- spring-tool-suite使用教程,并创建spring配置文件
本文为博主原创,未经允许不得转载: 在应用springMVC框架的时候,每次创建spring的xml配置文件时,需要很多步骤,非常麻烦. 所以spring提供了spring-tool-suite插件, ...
- 主键非自增列 EF 插入数据库引起的 ID 列不能为 NULL 的错误
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<PostBo ...
- 【Mysql】【Navicat For Mac】Navicat Premium for Mac v12.0.23 + macOS Sierra 10.12.6
参考地址:https://blog.csdn.net/womeng2009/article/details/79700667 [备注]我只用到了部分信息,就激活了 内容: Navicat Premiu ...
- 【BZOJ】3140: [Hnoi2013]消毒
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3140 猜一发(显然)有结论:每次一定选择一个平面,即每次操作对答案的贡献都为$1$ 首先可 ...
- win10上安装keras
下载Anaconda https://www.anaconda.com/ 点击进入下载界面 选择Windows版本64位,python3.7 下载完成后 ,双击安装 等待安装完成! 安装MinGW包, ...
- MySQL基本使用
来自李兴华视频. 1. 启动命令行方式 2. 连接mysql数据库,其中“-u”标记的是输入用户名,“-p”标记的是输入密码. 3. 建立一个新数据库——mldn,使用UTF-8编码: create ...
- 如何连接oracle 12c可插拔数据库
启动根容器:[oracle@eric ~]$ export ORACLE_SID=cup[oracle@eric ~]$ sqlplus / as sysdbaSQL*Plus: Release 12 ...
- [转][JSBSim]JSBSim的使用--飞行控制组件及其配置
http://www.jianshu.com/p/b5e9f1f5df95 飞行控制率.稳定增强系统.自动驾驶仪和其他飞控系统(航电.电气等)都能够在 JSBSim 中以独立的控制组件进行建模.JSB ...
- Codeforces 841 D - Leha and another game about graph
D - Leha and another game about graph 思路:首先,如果所有点的度数加起来是奇数,且没有-1,那么是不可以的. 其他情况都可以构造,我们先dfs出一个生成树,然后从 ...