(待解决,效率低下)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 ...
随机推荐
- Python有趣现象(不定时更新)
1.list中extend方法有趣现象 1.1 List+=Str 与 List.extend(Str) list1 = [11,2,45] str1 = 'Michael' list1.extend ...
- CAS 单点登录 移动端获取TGT、ST 已经移动端登录页面不进行跳转的设置
一.设置移动客户端验证ST通过后,页面不进行302重定向跳转 修改web.xml <!--**************************************************** ...
- 如何某个js文件中的 console
因为自己引用了别人的一个 js 文件,但里面有很多事件相关的 console 输出.自己并不想去修改别人的文件.但想屏蔽掉里面的 console . 有多个 js 文件里有 console.log . ...
- JavaScript深入
BOM(浏览器对象模型)——与浏览器对话: Window对象(代表浏览器的窗口——不包括工具栏.滚动条): //所有全局对象.全局函数,均自动成为window对象的成员(document属于浏览器,所 ...
- SyncDictionary
using System; using System.Collections; using System.Collections.Generic; using System.Threading; us ...
- C#引用出错
今天有朋友问我为什么自己引用了配置文件,但是还不能使用配置文件呢? 之后我查看他的项目,后来发现如下问题,并且总结引用文件流程如下: 引用文件的完整程序如下: 用配置文件举例 项目中的引用右击,然后点 ...
- OpenModelica Debug
assertion只触发一次 The gdb process has not responded to a command within 40 second(s).This could mean it ...
- 使用bat文件执行sql文件
test.bat mysql -uroot -p[password] < test.sql pause test.sql CREATE DATABASE IF NOT EXISTS test_d ...
- Spring的一个入门例子
例子参考于:Spring系列教材 以及<轻量级JavaEE企业应用实战> Axe.class package com.how2java.bean; public class Axe { p ...
- JAVA经典面试题:讲一讲JVM的组成
JVM(Java 虚拟机)算是面试必问的问题的了,而但凡问 JVM 一定会问的第一个问题就是:讲一讲 JVM 的组成?那本文就注重讲一下 JVM 的组成. 首先来说 JVM 的组成分为,整体组成部分和 ...