leetcode || 46、Permutations

problem:

Given a collection of numbers, return all possible permutations.

For example,

[1,2,3] have the following permutations:

[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2],
and [3,2,1].

Hide Tags

Backtracking

题意:给定一个序列,输出其所有的排列组合

thinking:

(1)之前写过求一个序列的下一个排列组合,这里就是应用求下一个排列的函数来输出所有可能的排列组合

(2)也可以调用STL的next_permutation()函数,这里我自己重新实现它

(3)题目标签提示使用回溯法,也可行,采用深度优先搜索算法

code:

dfs方法:

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int> > ret;
		dfs(ret, num, 0);
		return ret;
    }

	void dfs(vector<vector<int> >& ret, vector<int>& num, int cur)
	{
		if(num.size() == cur)
		{
			ret.push_back(num);
		}
		else
		{
			for(int i = cur; i < num.size(); ++i)
			{
				swap(num[cur], num[i]);
				dfs(ret, num, cur+1);
				swap(num[cur], num[i]);
			}
		}
	}
};

next_permutation方法:

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        vector<vector<int> > ret;
        vector<int> tmp;
        int n = num.size();
        int m=1;
        if(num.size()<2)
        {
            ret.push_back(num);
            return ret;
        }
        sort(num.begin(),num.end());
        while(n)
        {
            m*=n;
            n--;
        }
        ret.push_back(num);//先插入排序好的序列
        for(int i=1;i<m;i++)//m-1个
        {
            tmp = my_next_permutation(num);
            ret.push_back(tmp);
            num=tmp;

        }
        return ret;
    }
protected:
/*
*思路:从后往前比较,比如1-2-3,则直接反转后两位得到1-3-2
*对于1-2-4-3,则从后往前比较,发现2<4,则从后往前寻找第一个大于2的数3,交换得到1-3-4-2,
*       再反转3之后的4-2部分得到:1-3-2-4
*/
    vector<int> my_next_permutation(vector<int> tmp)
    {
        vector<int>::iterator i = tmp.end()-1;
        vector<int>::iterator j=i-1;
        vector<int>::iterator swap=i;
        if(*i>*j)
        {
            iter_swap(i,j);
            return tmp;
        }
        while(i!=tmp.begin() && *j>*i)
        {
            j--;
            i--;
        }
        if(i==tmp.begin())
        {
            reverse(tmp.begin(),tmp.end());
            return tmp;
        }
       while(swap!=j && *swap<=*j)
           swap--;
       iter_swap(j,swap);
       reverse(i,tmp.end());
       return tmp;
    }
};
时间: 2024-10-09 16:17:52

leetcode || 46、Permutations的相关文章

LeetCode 46:Permutations

Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. 这题不知道出了什么意外,输入[0,1]在Eclipse上运行结果:[[0, 1], [1, 0]] LeetCode上运行结果:[[1],[0,1],

[LeetCode 46 &amp; 47] Permutations I &amp; II

题目链接:permutations 相似题型: 1. [LeetCode 39&40] Combination Sum I & II 2. [LeetCode 78] Subsets 3. [LeetCode 90] Subsets II 4. [LeetCode 22] Generate Parentheses 5. [LeetCode 77] Combinations import java.util.ArrayList; import java.util.List; /** * Gi

leetcode || 47、 Permutations II

problem: Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. Hide Tags Backtracking 题意:给定一个带重复数字的序列,输出其所有的排列

[Leetcode 46]全排列 Permutations 递归

[题目] Given a collection of distinct integers, return all possible permutations. 数组的组合情况. Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] [思路] 求子集,排列组合的数组题有固定模板. [代码] class Solution { public List<List<Integer>> p

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1

LeetCode 46 Permutations(全排列问题)

题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组能够产生的所有全排列 采用递归算法,传入参数 List<List<Integer>> list, List<Integer> tempList, int[] nums, boolean[] used 其中list保存最终结果 tempList保存其中一个全排列 nums为最初的

LeetCode 46 Permutations (全排列)

Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 题目链接:https://leetcode.com/problems/permutations/ 题目大意:求全排列 题目分析:求

46、动态存储类

动态存储类 StrVec Class Design StrVec Class Definition class StrVec { public: //构造函数 StrVec():elements(nullptr), first_free(nullptr), cap(nullptr){} //用initializer_list<string>初始化参数列表 StrVec(initializer_list<string> il):StrVec(il){} //拷贝构造函数 StrVec

leetcode || 118、Pascal&#39;s Triangle

problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] Hide Tags Array 题意:帕斯卡三角形,又名杨辉三角形,是多项式(a+b)^n的系数 thinking: (1)杨辉三角形,从第三行开始,排除第一个和最后一个1外,其值