permutation II (boss出来了)
题目链接: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出来了)的更多相关文章
- 267. Palindrome Permutation II
题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...
- leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [Swift]LeetCode267.回文全排列 II $ Palindrome Permutation II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- LeetCode Palindrome Permutation II
原题链接在这里:https://leetcode.com/problems/palindrome-permutation-ii/ 题目: Given a string s, return all th ...
- [LeetCode#267] Palindrome Permutation II
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- [Locked] Palindrome Permutation I & II
Palindrome Permutation I Given a string, determine if a permutation of the string could form a palin ...
随机推荐
- html 报表导出excel防止数字变科学计数
在html 标签加: <html xmlns:x="urn:schemas-microsoft-com:office:excel"> 在要导出的tr加: &l ...
- 分享一个web存储方法
https://github.com/zhujiasheng/jquery-cookie/blob/master/src/jquery.cookie.js https://github.com/WQT ...
- 如何解救在异步Java代码中已检测的异常
Java语言通过已检测异常语法所提供的静态异常检测功能非常实用,通过它程序开发人员可以用很便捷的方式表达复杂的程序流程. 实际上,如果某个函数预期将返回某种类型的数据,通过已检测异常,很容易就可以扩展 ...
- Yii;CodeIgniter;thinkphp学习
http://www.ibm.com/developerworks/cn/opensource/os-cn-yii/ http://codeigniter.org.cn/ http://baike.b ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework概述
Spring Framework是什么? it is a potential one-stop-shop for building your enterprise-ready applications ...
- AWS Redshift summary
https://blogs.aws.amazon.com/bigdata/post/Tx31034QG0G3ED1/Top-10-Performance-Tuning-Techniques-for-A ...
- jQuery与Ajax的应用——《锋利的jQuery》(第2版)读书笔记3
第6章 jQuery与Ajax的应用 jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第2层是load().$.get()和$.post()方法,第3层是$ ...
- 第1周 支路变量、元件、KCL和KVL
第1周的内容,介绍了: 电阻.独立源.受控元件等实体元器件, 电流.电压.功率等抽象名词, 端口.参考方向等分析时的概念工具, KCL.KVL两大分析定律, 解线性电路的普适方法----2B法. 引入 ...
- 获取WIFI的SSID和本机IP
1.获取WIFI的SSID 引入库 #import <SystemConfiguration/CaptiveNetwork.h> ..... ..... // WIFI的名字 + (NSS ...
- WCF use ProtoBuf
ProtoBuf, 比起xml和json, 传输的数据里面没有自描述标签, 而且是基于二进制的, 所以有着超高的传输效率, 据牛人张善友的描述, 可以替代WCF的自带的编码方案, 效率有极大的提升. ...