题目链接:https://leetcode.com/submissions/detail/55876321/

自己的做法,30个测试用例通过了29例,终究还是有一个系列类型的是无法通过的,因为自己妄想在permutation的代码上,通过排序来进行。然而,每一次同第一个元素交换位置之后,进入了递归,此时数组nums并不是有序的!!!

来看看自己的这段代码,谨记教训,然后还是去看看大神们的思路吧!

class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
if(nums.size()==)
return res;
sort(nums.begin(),nums.end());//事实
int len=nums.size();
vector<int> temp;
helper(nums,,,len,temp);
return res;
}
private:
void helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp);
private:
vector<vector<int>> res;
}; void Solution::helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp){
if(count==len){
res.push_back(temp);
return;
}
for(int i=pos;i<len;i++){
if(pos!=i && nums[pos]==nums[i])//后面元素与自己相等时,忽略此次递归
continue;
if(i>pos&&nums[i]==nums[i-])//当后面有连续相同的元素存在时,只做第一次,后面的相等元素忽略
continue;
if(pos!=i)
swap(nums[pos],nums[i]);
temp.push_back(nums[pos]);//这儿可别写成了nums[i],害自己调试半天
helper(nums,pos+,++count,len,temp);
temp.pop_back();
count--;
swap(nums[pos],nums[i]);
}
}

http://www.cnblogs.com/TenosDoIt/p/3662644.html

参考了大神的博客,再修改自己的代码:

 class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
if(nums.size()==)
return res;
// sort(nums.begin(),nums.end());//事实
int len=nums.size();
vector<int> temp;
helper(nums,,,len,temp);
return res;
}
private:
void helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp);
bool find(vector<int>&nums,int begin,int end,int target);
private:
vector<vector<int>> res;
}; void Solution::helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp){
//在上一算法的基础上,当我们枚举第i个位置的元素时,若要把后面第j个元素和i交换,则先要保证[i…j-1]范围内没有和位置j相同的元素。有以下两种做法(1)可以每次在需要交换时进行顺序查找;(2)用哈希表来查重。具体见下面的代码。
if(count==len){
res.push_back(temp);
return;
}
for(int i=pos;i<len;i++){
// if(pos!=i && nums[pos]==nums[i])//后面元素与自己相等时,忽略此次递归
// continue;
// if(i>pos&&nums[i]==nums[i-1])//当后面有连续相同的元素存在时,只做第一次,后面的相等元素忽略
// continue;
// if(pos!=i)
if(i>pos&&find(nums,pos,i-,nums[i]))//第一次时竟然写成了i,于是乎每一次都会执行continue!!!!
continue;
swap(nums[pos],nums[i]);
temp.push_back(nums[pos]);//这儿可别写成了nums[i],害自己调试半天
helper(nums,pos+,++count,len,temp);
temp.pop_back();
count--;
swap(nums[pos],nums[i]);
}
}
bool Solution::find(vector<int>&nums,int begin,int end,int target){
for(;begin<=end;begin++){
if(nums[begin]==target)
return true;
}
return false;
}

permutation II (boss出来了)的更多相关文章

  1. 267. Palindrome Permutation II

    题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...

  2. leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II

    266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...

  3. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  4. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  5. [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  6. [LeetCode] 267. Palindrome Permutation II 回文全排列 II

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  7. LeetCode Palindrome Permutation II

    原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...

  8. [LeetCode#267] Palindrome Permutation II

    Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  9. [Locked] Palindrome Permutation I & II

    Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...

随机推荐

  1. Python第二模块(文件和函数)

    1. 集合操作    集合的特点:无序,不重复的数据组合 集合的作用: 去重,将列表变为集合,就会自动去重 关系测试,测试两组数据之间的交集.差集.并集关系 常用操作: #创建集合 s = {1,2, ...

  2. 什么是SCADA Viewer

    SCADA Viewer 什么是SCADA Viewer SCADA Viewer是一个基于Web的软件框架(基于Web的HMI/SCADA/M2M工业和楼宇自动化,支持Modbus,BACnet,O ...

  3. SQL Server更新表(用一张表的数据更新另一张表的数据)

    a) 写法轻松,更新效率高: update table1 set field1=table2.field1,field2=table2.field2 from table2 where table1. ...

  4. 驱动开发学习笔记. 0.04 linux 2.6 platform device register 平台设备注册 1/2 共2篇

    驱动开发读书笔记. 0.04  linux 2.6 platform device register 平台设备注册  1/2 共2篇下面这段摘自 linux源码里面的文档 : Documentatio ...

  5. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

  6. 接入百度语音SDK的步骤

    1.导入依赖库 SystemConfiguration.framework AudioToolbox.framework UIkit.framework AVFoundation.framework ...

  7. c语言折半查找

    折半查找又称为二分查找,它的前提是线性表中的记录必须是有序的(通常从小到大有序),线性表必须采用顺序存储. 折半查找的基本思想是 : 在有序表中,取中间记录作为比较对象,若给定值与中间记录的关键字相等 ...

  8. setValue和setObject的区别

    在NSMutableDictionary的方法中有setValue forKey与setObject forKey,它们都可以用来设置某一个key值对应的value 1,setValue: forKe ...

  9. HandlerThread源码分析

    其实原本HandlerThread的分析不应该单独开一篇博客的,应该在讲消息机制的那一片中一起分析. 但当时忘记了,而且今天第一次用MarkDown写博客,有点上瘾,就再来一篇,权当滥竽充数过过手瘾. ...

  10. Ext.Net TreePanel 修改Icon图标

    分类: Ext.Net2012-09-24 13:44 1779人阅读 评论(0) 收藏 举报 webformserverextassemblyxhtmlobject 1.默认icon 2.自定义ic ...