46. Permutations (Back-Track,Sort)
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<vector<int> > permute(vector<int> &num) {
        result.clear();
        dfs(num,);
        return result;
    }
    void dfs(vector<int> num, int depth)
    {
        if(depth == num.size()-)
        {
            result.push_back(num);
            return;
        }
        dfs(num,depth+);
        int temp = num[depth];
        for(int i = depth+;i< num.size(); i++)
        {
            num[depth] = num[i];
            num[i] = temp;
            dfs(num,depth+);
            num[i] = num[depth];
            num[depth] = temp;
        }
    }
private:
    vector<vector<int> >  result;
};
思路II:
当字符串长度为2时 a1a2 a2a1
当字符串长度为3时 a3a1a2 a1a3a2 a1a2a3 a3a2a1 a2a3a1 a2a1a3
比较可以得到 其实就是把a3(多出来的元素)插在长度为2时的两个字符串的任意位置
时间复杂度:三个for循环 O(n3)
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        int size = nums.size();
        int resultSize;
        int resultIndex;
        vector<vector<int>> result;
        vector<int> resultItem(,nums[]);
        result.push_back(resultItem);
        for(int i = ; i <size; i++){ //nums[i] is the num to insert
            resultSize = result.size(); //resultSize in the preceeding insert iterate
            for(int j = ; j < resultSize; j++){ //iterate the array to do insertion
                result[j].push_back(nums[i]);
                resultIndex = j;
                for(int k = i-; k >=; k--){ //like insertion sort, adjust forward
                    result.push_back(result[resultIndex]);
                    result[result.size()-][k+] = result[resultIndex][k];
                    result[result.size()-][k] = result[resultIndex][k+];
                    resultIndex = result.size()-;
                }
            }
        }
        return result;
    }
};
46. Permutations (Back-Track,Sort)的更多相关文章
- LeetCode - 46. Permutations
		46. Permutations Problem's Link -------------------------------------------------------------------- ... 
- [Leetcode][Python]46: Permutations
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ... 
- 46. Permutations 排列数
		46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ... 
- 刷题46. Permutations
		一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ... 
- [LeetCode] 46. Permutations 全排列
		Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ... 
- 46. Permutations
		题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ... 
- 【一天一道LeetCode】#46. Permutations
		一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ... 
- 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence
		▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ... 
- 【LeetCode】46. Permutations (2 solutions)
		Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ... 
随机推荐
- ESET Smart Security – 免费90天(sv)
			Eset 活动很多,还有beta测试,几乎可以免费使用Eset.萨尔瓦多.免费活动,3个月免费ESS/EAV活动地址: 点此进入 填写表格,给你免费3个月的 ESS\EAV 许可证.官方网站: 点此 ... 
- 本地Jmeter脚本部署在Jenkins上 - Windows
			一.下载并安装Jenkins(不进行特别的说明) 二.准备好jmeter脚本 三.插件准备:Publish HTML reports 四.开始 1.登录Jenkins后,点击新建任务 2.输入项目名, ... 
- Xadmin的配置及使用
			xadmin是Django的第三方扩展,可是使Django的admin站点使用更方便. 1. 安装 通过如下命令安装xadmin的最新版 pip install https://github.com/ ... 
- FOR UPDATE
			1. for update的使用场景 `如果遇到存在高并发并且对于数据的准确性很有要求的场景,是需要了解和使用for update的. 比如涉及到金钱.库存等.一般这些操作都是很长一串并且是开启 ... 
- CUDA  安装完成以后如何判断安装是否成功
			最近在家里过寒假,可能这是还在学校里带着最大的福利了,无意之中翻出了多年前买的几本关于CUDA编程的书,于是随便在自己电脑上配置了一下环境,试试能不能把当年没有看完的书给看完了,于是有了今天这个判断C ... 
- c++ 读取所有图片
			copyright by Jun Yang, SUN YAT-SEN UNIVERSITY //FileList.h ///////////////////////////////////////// ... 
- 《DSP using MATLAB》Problem 2.6
			1.代码 %% ------------------------------------------------------------------------ %% Output Info abou ... 
- oracle用expdp定时备份所有步骤详解[转]
			用oracle命令备份数据库,生成dmp文件,保存了整一套的用户及表数据信息.还原简单.加上widnows的批处理bat命令,实现每天0点备份,现把经验送上给大家! 工具/原料 oracle11g ... 
- win10 下ie11安装flash debuger (install   flashplayer  debuger  on   win10 64bit)
			1不能安装的现象 由于win10 ie11 内置flash 微软不让用户自己手动更新ie11的flash以及安装flash debugger ,这怕是让还在用 flex 开发的大胸弟们很头疼 ... 
- oracle之 RAC 11G ASM下控制文件多路复用
			如果数据库仅有一组control file文件,需要添加一组或者多组,保证一组文件损坏或者丢失导致数据库宕机. -- 环境说明SQL> select * from v$version;BANNE ... 
