<LeetCode OJ> 46. Permutations

46. Permutations

My Submissions

Question

Total Accepted: 81495 Total
Submissions: 239854 Difficulty: Medium

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],
and [3,2,1].

Subscribe to see which companies asked this question

Hide Tags

Backtracking

Hide Similar Problems

(M) Next Permutation (M)
Permutations II
 (M) Permutation Sequence (M)
Combinations

//思路首先:算了,还是用stl来做吧
//再次来看nextPermutation的原理:
//但是比较明显的规律是:
//1,升序为最小组合数,降序为最大组合数
//2,某一个数的下一个数,就是比他大的最小组合数,比如123,下一个比他大的最小组合就是132,所以必须从低位处理
//3,找到首个升序序列将其交换,然后逆置后面的数。比如:1234311
//为1243311(首个升序已经交换),显然此数不是最小,并且后面的数逆置后才是最小的,即1243311为1243113。so,done!。
//参考我的另一篇文章:http://blog.csdn.net/ebowtang/article/details/50450861
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        int col=nums.size();
        int row=1;
        for(int i=1;i<=col;i++)//求阶乘
            row*=i;
        vector< vector<int> >  result(row);
        for(int i=0;i < row ;i++)
            result[i].resize(col);//设置数组的大小row行,col列
        if(nums.empty())
            return result;
        vector<int> tmpnums=nums;
        sort(tmpnums.begin(),tmpnums.end());//先排序
        result[0]=tmpnums;
        for(int i=1;i<row;i++)
        {
            next_permutation(tmpnums.begin(), tmpnums.end());
            result[i]=tmpnums;
        }
        return result;
    }
};

注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50488439

原作者博客:http://blog.csdn.net/ebowtang

时间: 2024-12-29 07:23:12

<LeetCode OJ> 46. Permutations的相关文章

[LeedCode OJ]#46 Permutations

[ 声明:版权所有,转载请标明出处,请勿用于商业用途.  联系信箱:[email protected]] 题目链接:https://leetcode.com/problems/permutations/ 题意: 给定一个数组,要求返回其所有全排列的情况 思路: 对于一个特定排列我们有一个求下一个全排列的函数,那就是next_permutation,运用这个函数这道题迎刃而解 class Solution { public: vector<vector<int> > permute(

LeetCode OJ: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]. 排列组合的问题,不用考虑是否发生重复的情况,代码如下: 1 class Solution { 2 public: 3 vector<vector<i

白菜刷LeetCode记-46. Permutations

今天这一题也是中等难度,题目如下: 这一题是要实现数组的全排列.这一题是要使用遍历以及递归的思想去实现,代码如下: 1 /** 2 * @param {number[]} nums 3 * @return {number[][]} 4 */ 5 var permute = function(nums) { 6 var res = new Array(); 7 8 helper(nums, 0, nums.length - 1 , res); 9 10 return res; 11 }; 12 1

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 回溯问题 | Medium

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] ] 题目大意:求一个序列的全排列. 思路:做排列

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/ 题目大意:求全排列 题目分析:求

[Lintcode]15. Permutations/[Leetcode]46. Permutations

15. Permutations/46. Permutations 本题难度: Medium Topic: Search & Recursion Description Given a list of numbers, return all possible permutations. Example Example 1: Input: [1] Output: [ [1]] Example 2: Input: [1,2,3] Output: [ [1,2,3], [1,3,2], [2,1,3]

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar