关闭

leetcode 46 : Permutations

标签: 面试算法leetcode
374人阅读 评论(0) 收藏 举报
分类:

http://blog.csdn.net/tuantuanls/article/details/8717262 分析的很好

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].


class Solution {
public:
    vector> permute(vector& nums) {
        vector> ret;
        vector backNums=nums;
        if(nums.size()==1)
        {
            ret.push_back(nums);
            return ret;
        }
        //for(int i=0;i> tmp=permute(nums);
//一直删除,在其他的递归层次中当前值都不存在,所以要插入各个位置(如果删除一个,就恢复nums,再循环删除下一个,
//到时只要插入头部即所求,因为是循环恢复删除当前过程中 删除的元素在其他的循环中会恢复包含在除去头部的任何位置
            for(int j=0;j backTmpJ=tmp[j];
                for(int k=0;k<=tmp[j].size();k++){
                    backTmpJ=tmp[j];
                    backTmpJ.insert(backTmpJ.begin()+k,backNums[0]);//将移除的nums.begin()插入各个位置
                    ret.push_back(backTmpJ);
                }
            }
        //}
        return ret;
    }
};
// 循环恢复删除
class Solution {
public:
    vector> permute(vector& nums) {
        vector> ret;
        vector backNums=nums;
        if(nums.size()==1)
        {
            ret.push_back(nums);
            return ret;
        }
        for(int i=0;i> tmp=permute(nums);
            for(int j=0;j
0
0
查看评论

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], [...
  • fly_yr
  • fly_yr
  • 2015-08-31 20:07
  • 1962

leetcode 46. Permutations-全排列|回溯|递归|非递归

原题链接:46. Permutations 【思路-Java、Python】递归实现 全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个。现以{1, 2, 3, 4, 5}为例说明如何编写全排列的递归算法。 1、首先看最后两个数4, 5。 它们的全排列为4 5和5 4, 即以...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-05-29 22:15
  • 3855

LeetCode(46) Permutations

题目如下: 分析如下: 我的代码:
  • feliciafay
  • feliciafay
  • 2014-11-21 15:54
  • 1318

[leetcode] 46. Permutations 解题报告

题目链接:https://leetcode.com/problems/permutations/ Given a collection of distinct numbers, return all possible permutations. For example, [...
  • qq508618087
  • qq508618087
  • 2016-01-14 16:14
  • 406

LeetCode46——Permutations

LeetCode46——Permutations 求全排列,这题偷了个懒,借用了之前求下一个全排列的代码。 之前看到过递归的方法,包含重复元素,和不包含重复元素的方法,有时间会补上。 代码: class Solution { private: void Swap(int &a, i...
  • zhangxiao93
  • zhangxiao93
  • 2015-10-23 09:21
  • 827

LeetCode(46): Permutations (C++)

一、题目     Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the following permutations: ...
  • zhangpiu
  • zhangpiu
  • 2015-12-01 10:00
  • 537

[leetcode-46]Permutations(java)

问题描述: 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,...
  • zdavb
  • zdavb
  • 2015-08-02 17:23
  • 1116

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],&...
  • bingbing8219
  • bingbing8219
  • 2015-06-24 11:11
  • 295

[LeetCode-Java]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...
  • qq_32199531
  • qq_32199531
  • 2016-10-19 22:23
  • 479

【leetcode】 46. Permutations ,python实现

Permutations Given a collection of distinct numbers, return all possible permutations.For example, [1,2,3] have the following permutations: [ [1...
  • zl87758539
  • zl87758539
  • 2016-06-15 15:43
  • 1584
    个人资料
    • 访问:160096次
    • 积分:3239
    • 等级:
    • 排名:第12452名
    • 原创:154篇
    • 转载:115篇
    • 译文:0篇
    • 评论:3条
    最新评论