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)的更多相关文章

  1. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  2. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  3. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...

  4. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  5. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  6. 46. Permutations

    题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...

  7. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  8. 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence

    ▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...

  9. 【LeetCode】46. Permutations (2 solutions)

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

随机推荐

  1. 公式中表达单个双引号【"】和空值【""】的方法及说明

    http://club.excelhome.net/thread-661904-1-1.html 有人问为什么不用三个双引号"""来表示单个双引号["]呢,如果 ...

  2. GPU编程自学7 —— 常量内存与事件

    深度学习的兴起,使得多线程以及GPU编程逐渐成为算法工程师无法规避的问题.这里主要记录自己的GPU自学历程. 目录 <GPU编程自学1 -- 引言> <GPU编程自学2 -- CUD ...

  3. iOS KVC 和 KVO 区别简单总结

    KVC: key value coding,键值编码.是一种通过使用属性的名称(key)来间接访问对象属性的方法.这个方法可以不用通过 setter/getter 方法来访问对象的属性.该方法使用的实 ...

  4. 一个Self Taught Learning的简单例子

    idea: Concretely, for each example in the the labeled training dataset xl, we forward propagate the ...

  5. 實驗項目wordcount

    wordcount 1.设计思路 第一步 :主函数参数使用命令行参数,定义一个文件指针fp. 第二步:判断能否用只读的形式打开命令行指针中的文件,并让指针指向打开函数,若不能则输出不能读取文件,否则下 ...

  6. HDU 2050:折线分割平面

    折线分割平面 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  7. 一行能装逼的JavaScript代码的延伸

    前段就是坑,入坑水真深. 先看看一个黑科技, 纳尼,这是什么东西. (!(~+[])+{})[--[~+""][+[]]*[~+[]] + ~~!+[]]+({}+[])[[~!+ ...

  8. 在css中使用hover来控制其他元素的样式,该两个元素必须是父子元素

    .col-3:hover .check-box { display: block; } 在css中使用hover来控制其他元素的样式,该两个元素必须是父子元素!!!!

  9. Reusing & Composing GraphQL APIs with GraphQL Bindings

    With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...

  10. ①Jenkins集成—入门安装使用

    一.什么是Jenkins jenkins是一个广泛用于持续构建的可视化web工具,持续构建说得更直白点,就是各种项目的"自动化"编译.打包.分发部署.jenkins可以很好的支持各 ...