题目链接: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. Spring boot配置文件 application.properties

    http://www.tuicool.com/articles/veUjQba 本文记录Spring Boot application.propertis配置文件的相关通用属性 # ========= ...

  2. mysql - 缺失范围和连续范围

    初始化数据 # 创建表 DROP TABLE IF EXISTS g; CREATE TABLE g( a INT )ENGINE=INNODB; # 初始化数据 ; ; ; ; ; ; ; ; ; ...

  3. vim的共享系统剪贴板以及缩进相关问题

    http://www.cnblogs.com/end/archive/2012/06/01/2531147.html:reg 可以显示可用的寄存器,其中注意两个特殊的寄存器:"* 和 &qu ...

  4. Redis学习笔记-初级

    1.Redis简介 Redis是一个高效缓存内存数据库,开源.免费.key-value格式 相比于其他key-value格式的缓存产品,特点/优势在于: 支持持久化,可以将内存中的数据保存在磁盘中,重 ...

  5. 主从LDAP

    yum -y install compat-openldap必须得安装这个   1:在主上       备份         cp /etc/openldap/slapd.conf /etc/open ...

  6. SpringMVC集成缓存框架Ehcache

    在互联网应用中,应用并发比传统企业及应用会高出很多.解决并发的根本在于系统的响应时间与单位时间的吞吐量.思路可分为:一减少系统的不必要开支(如缓存),二是提高系统单位时间内的运算效率(如集群). 在硬 ...

  7. ww

    #!bin/python #coding=utf-8 """ Create by he """ import sys import re i ...

  8. netcat使用

    一.端口监听(实时消息) 首先在A计算机上,它充当的是服务器角色,$ nc -l 3333 这时就创建了一个监听端口(listening socket(server)).- -l 它让 nc 监听一个 ...

  9. Js制作的文字游戏

    自己制作的文字游戏.(: <!DOCTYPE html><html lang="en"><head>    <meta charset=& ...

  10. jQuery Mobile 图标

    jQuery 图标 如需在 jQuery Mobile 中向按钮添加图标,请使用 data-icon 属性: <a href="#anylink" data-role=&qu ...