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 ...
随机推荐
- [转载]java中的标号:outer的作用
转载自:http://blog.sina.com.cn/s/blog_6f8bd746010136yr.html 标号label 标号提供了一种简单的break语句所不能实现的控制循环的方法,当在循环 ...
- I.MX6 U-boot lvds display hacking
/*********************************************************************************** * I.MX6 U-boot ...
- Ubuntu下gcc的简单使用
一直不怎么用gcc,今天看了大神们的笔试题,不得不动手开始写程序了,差距那个大啊. gcc是ubuntu下的终端编译器,可以用来写C.C++的程序,简单用法如下: vim name1.c 先用vim打 ...
- BZOJ4833: [Lydsy1704月赛]最小公倍佩尔数(min-max容斥&莫比乌斯反演)(线性多项式多个数求LCM)
4833: [Lydsy1704月赛]最小公倍佩尔数 Time Limit: 8 Sec Memory Limit: 128 MBSubmit: 240 Solved: 118[Submit][S ...
- Java其他API介绍
有一些类虽然不像集合.多线程.网络编程中的类那样属于Java中的核心类,但是它们在开发过程中给我们带来很多便利,这里就对它们做下简要的介绍和演示. 一.System类 System类中的构造方法是私有 ...
- Linux性能评估命令
Linux性能评估工具 https://www.cnblogs.com/dianel/p/10085454.html Linux性能评估工具 目录 介绍 负载:uptime 查看内核的信息: dmes ...
- 获取bing带swim的网址列表
需求背景: 应老婆要求,搜集带有swim关键字的网站.实现过程: 使用requests模块通过bing接口搜索swim关键,将返回内容按需求进行处理,得到网站列表. 注:代码比较拙,老司机就不要弄废时 ...
- bzoj2257瓶子和燃料
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2257 以两个瓶子为例,可以倒出它们的差,这是它们容量的gcd的倍数. k个瓶子就可以倒出它们 ...
- JProfiler 8(一个很好的java性能监控工具) 下载和注册码
windows x64 zip下载地址:http://download-aws.ej-technologies.com/jprofiler/jprofiler_windows-x64_8_0_1.zi ...
- WCF揭秘学习笔记(1):基础知识
最近找工作,面试时经常被问懂不懂WCF.不少招聘高级.NET工程师的要求上都 写着有WCF开发经验的优先考虑.我对于WCF仅仅是通过看一些教学视频这种山寨学习法了解一些.现在要下决心好好学习一下WCF ...