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 ...
随机推荐
- 关于面向对象和String类型的 09,10
package test.面试题; public class Test9 { public static void main(String[] args){ Outer.Inner in=new Ou ...
- 再谈Spring AOP
1.AOP的基本概念 在进行AOP开发前,先熟悉几个概念: 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化.方法执行.方法调用.字段调用或处理异常等等,S ...
- vim没有权限却可以强制保存时所引起的思考 ------ 文件夹权限对所属文件的权限影响
最近在拿着Linux 鸟叔私房菜对着Linux 系统学习一下基本操作,虽然已经使用Linux系统已经好多年不过却一直没有系统的学习一下.在用vim 编辑一个文件的时候出现了一个很神奇的事情,明明该文件 ...
- MySQL 存储过程理解
/********************************************************************************* * MySQL 存储过程理解 * ...
- Codeforces 148B: Escape
题目链接:http://codeforces.com/problemset/problem/148/B 题意:公主从龙的洞穴中逃跑,公主的速度为vp,龙的速度为vd,在公主逃跑时间t时,龙发现公主逃跑 ...
- 【C#】App_LocalResources实现多语言
介绍 如果您创建的网页将由使用不同语言的用户阅读,则必须为这些读者提供用他们自己的语言查看网页的方法.一种方法是分别用各语言重新创建页面,但这种方法可能需要大量工作量.容易出错并且在更改原始页时很难维 ...
- 在css中使用hover来控制其他元素的样式,该两个元素必须是父子元素
.col-3:hover .check-box { display: block; } 在css中使用hover来控制其他元素的样式,该两个元素必须是父子元素!!!!
- 无线密码破解----minidwep-gtk的PIN破解方法
使用虚拟机对minidwep-gtk进行PIN破解 用CDLINUX支持8187和3070_30211版.iso系统PJpin码 1.用虚拟机的好处是方便,可以一边破解,一边上网做其他事情. 虚拟机 ...
- errno.h的数字对应的字符串错误
#ifndef _I386_ERRNO_H #define _I386_ERRNO_H #define EPERM 1 /* Operation not permitted */ #define EN ...
- FastAdmin 升级后出现 is already in use
FastAdmin 升级后出现 is already in use 升级 FastAdmin 改进很多,但全新安装出现以下错误 Cannot use app\common\library\Menu a ...