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()==0)
            return res;
        sort(nums.begin(),nums.end());//事实
        int len=nums.size();
        vector<int> temp;
        helper(nums,0,0,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-1])//当后面有连续相同的元素存在时,只做第一次,后面的相等元素忽略
            continue;
        if(pos!=i)
        swap(nums[pos],nums[i]);
        temp.push_back(nums[pos]);//这儿可别写成了nums[i],害自己调试半天
        helper(nums,pos+1,++count,len,temp);
        temp.pop_back();
        count--;
        swap(nums[pos],nums[i]);
    }
}

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

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

 1 class Solution {
 2 public:
 3     vector<vector<int>> permuteUnique(vector<int>& nums) {
 4         if(nums.size()==0)
 5             return res;
 6 //        sort(nums.begin(),nums.end());//事实
 7         int len=nums.size();
 8         vector<int> temp;
 9         helper(nums,0,0,len,temp);
10         return res;
11     }
12 private:
13     void helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp);
14     bool find(vector<int>&nums,int begin,int end,int target);
15 private:
16     vector<vector<int>> res;
17 };
18
19 void Solution::helper(vector<int>& nums,int pos,int count,int len,vector<int>& temp){
20     //在上一算法的基础上,当我们枚举第i个位置的元素时,若要把后面第j个元素和i交换,则先要保证[i…j-1]范围内没有和位置j相同的元素。有以下两种做法(1)可以每次在需要交换时进行顺序查找;(2)用哈希表来查重。具体见下面的代码。
21     if(count==len){
22         res.push_back(temp);
23         return;
24     }
25     for(int i=pos;i<len;i++){
26 //        if(pos!=i && nums[pos]==nums[i])//后面元素与自己相等时,忽略此次递归
27 //            continue;
28 //        if(i>pos&&nums[i]==nums[i-1])//当后面有连续相同的元素存在时,只做第一次,后面的相等元素忽略
29 //            continue;
30 //        if(pos!=i)
31         if(i>pos&&find(nums,pos,i-1,nums[i]))//第一次时竟然写成了i,于是乎每一次都会执行continue!!!!
32             continue;
33         swap(nums[pos],nums[i]);
34         temp.push_back(nums[pos]);//这儿可别写成了nums[i],害自己调试半天
35         helper(nums,pos+1,++count,len,temp);
36         temp.pop_back();
37         count--;
38         swap(nums[pos],nums[i]);
39     }
40 }
41 bool Solution::find(vector<int>&nums,int begin,int end,int target){
42     for(;begin<=end;begin++){
43         if(nums[begin]==target)
44             return true;
45     }
46     return false;
47 }
时间: 2024-11-07 15:47:57

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

267. Palindrome Permutation II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

Palindrome Permutation II 解答

Question Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s

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

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s = "a

lintcode-medium-Next Permutation II

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). Example Here

[LeetCode#267] Palindrome Permutation II

Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. For example: Given s = "aabb", return ["abba", "baab"]. Given s

Lintcode190 Next Permutation II solution 题解

[题目描述] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). 给定一个若干

[LeetCode]Palindrome Permutation II

递归做法 public class Solution { List<String> result = new ArrayList<String>(); public List<String> generatePalindromes(String s) { HashMap<Character, Integer> map = new HashMap<Character, Integer>(); for (int i = 0; i < s.len

Palindrome Permutation I &amp; II

Palindrome Permutation I Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even

[LeetCode] Palindrome Permutation I &amp; II

Palindrome Permutation Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even le