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 ...
随机推荐
- jshint创建配置文件
在项目中创建文件,并必须以 .jshintrc 命名: 例如 { "eqeqeq":true, "curly":true}
- NodeJS搭建HTTPS服务器
[NodeJS搭建HTTPS服务器] http://cnodejs.org/topic/54745ac22804a0997d38b32d
- PTA Sort Three Distinct Keys
Suppose you have an array of N elements, containing three distinct keys, "true", "fal ...
- laravel(二):laravel基本入门 看到Hello Laravel
1.Hello World 首先,我们来添加一些文字,在页面中显示.为了能访问网页,要启动程序服务器. $ php artisan serve 上述命令会启动 PHP 内建的开发服务器,要查看程序,请 ...
- 轻型的ORM类Dapper
Dapper是一个轻型的ORM类.代码就一个SqlMapper.cs文件,主要是IDbConnection的扩展方法,编译后就40K的一个很小的dll.官方站点http://code.google.c ...
- es6之set和map
1.set ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值,set本身就是构造函数,所以可以通过new的方式来创建对象 // 例一 可以做数组去重用 var set ...
- mac 端口转发方案
mac 端口映射 好坑 osx10.10mac移除了ipfw改用pfpf以前没用过 查文档mac的 pf语法,没有找到.bsd的倒是很全,心喜拿来一试.http://www.openbsd.org/f ...
- Fedora22编译Qt3.3.X
FC22编译QT3 安装g++ dnf install gcc-c++ 1,configure ./configure -thread -no-nis -system-libpng -system-l ...
- 使用angularJS遇见的一些问题的解决方案
1. angularJS的$http.post请求,SpringMVC后台接收不到参数值的解决方案 问题一般为:400 Required String parameter 'rPassword' is ...
- 文件上传——servlet实现
自己对照别的博主的博客实现的,记录用. 整个上传的结构如下: 上传的页面:unload.jsp <%@ page language="java" import="j ...