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 ...
随机推荐
- zstuoj 4243 牛吃草 ——(二分+两圆交)
这题上次补了以后忘记写博客了,现在补一下. 有两个注意点,第一是两圆相交的模板.可以通过任意一种情况手推出来. 第二是,实数二分要注意不用ans记录为妙,因为可能因为eps过小,导致ans无法进入记录 ...
- Java file read & write
1. read public static void readfile(String filepath) { BufferedReader br = null; try { String sCurre ...
- MLA Handbook for Writers of Research Papers笔记
MLA Handbook for Writers of Research Papers.7th ed.New York:MLA,2009.print.还有一本,留待阅读MLA Style Manual ...
- CURL详解(转载)
curl_setop()函数中的参数中文说明 curl_setop()函数中的参数中文说明 curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,value是这个 ...
- [OC笔记]@property之个人理解,大神轻拍
/** * 一个简单的对象 * * @author suzhen * */ public class SimpleObjcet { /** * 声明一个age字段 */ private Object ...
- chrome浏览器开发者工具之同步修改至本地
相信好多小伙伴喜爱webpack的热加载技术,省时而又不繁琐,讨厌F5或者Ctrl+F5. 嘿嘿,现在介绍大家一个在浏览器中修改直接同步到本地代码修改的方法--- (程序员都是从0开始数数的!) 第0 ...
- SpringMVC对异常进行全局处理,并区分对待ajax和普通请求
异常信息应统一进行处理. 程序员开发过程中,应尽量少用try..catch.避免因为catch造成的业务歧义.而在web开发中,普通的页面提交动作,和ajax提交动作,处理方式不一样,因为跳转后直接显 ...
- c# long转 datetime
; DateTime start = , , , , , , DateTimeKind.Utc); DateTime date = start.AddMilliseconds(time).ToLoca ...
- java class的兼容问题
前不久在工作中,遇到了几次编译class引起的NoSuchMethodError,经过分析与测试验证,也算是搞清楚了中间的来龙去脉,现在把一些结论性的东西(附带一些过程性的分析)分享出来. 在使用ja ...
- PHP获取页面执行时间的方法
一些循环代码,有时候要知道页面执行的时间,可以添加以下几行代码到页面头部和尾部: 头部: <?php $stime=microtime(true); 尾部: $etime=microtime(t ...