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

求队列的"排列"(相似的Leetcode中还有队列"组合"算法:Combination)。典型的使用回溯法。

回溯法都是求队列的排列组合,可分两种情况:

1、一种是元素集合成员少,但是每个元素多次出现,则按照一定规律填充队列,例如Generate Parentheses
2、一种是元素集合成员多,但是每个元素出现一次,则按照不断交换的方法重组队列,复杂度和排列组合计算的过程一样:n*n-1*n-2*...,例如本题;

对于排列组合题目:

1、组合的算法,可以按照不断填充的方法;

2、排列的算法,不能不断填充,因为无序,可以使用交换的方法;

本题解题思路:

假设ABCDE的全排列,记为P(ABCDE)

那么P(ABCDE) = {A P(BCDE)},{B P(ACDE)},{C P(BADE)},{D P(BCAE)},{E P(BCDA)};

也就是说,ABCDEF的全排列 = A与BCDE的全排列 + B与ACDE的全排列,...

同理P(BCDE) = {B P(CDE)},{C P(BDE)},{D P(CBE)},{E P(CDB)};

....

最终迭代到最后一层的结果,就是全排列的结果;

解题步骤:

1、主函数新建一个二维数组,用于保存全排列结果

2、调用编写的全排列子函数。

3、编写一个求全排列的函数,输入为结果保存集合res_lst(引用传值)、数字集合nums(非引用传值,因为每次迭代都不一样)、待排列的数字起始下标idx。

  (1)如果idx = nums.size(),说明已经完成最后一层的全排列,当前nums的排列方式即为一种全排列,将nums装入lst;

  (2)从int i = idx遍历到i = 数组尾:

    a. 交换nums[idx]和nums[i],即把待排列的每个数字置换出去,对剩下的数字递归全排列;

    b. 递归调用求全排列函数,对idx+1及其之后的数字进行全排列;

    c. 还要将nums[idx]和nums[i]交换回来,因为不能影响循环下面的交换操作

代码:

 class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> lst;
Backtracking(lst, nums, );
return lst;
} void Backtracking(vector<vector<int>> &lst, vector<int> nums, int idx) {
if (idx == nums.size() - 1) {
lst.push_back(nums);
return;
} for (int i = idx; i < nums.size(); ++i) {
swap(nums[idx], nums[i]);
Backtracking(lst, nums, idx+);
swap(nums[idx], nums[i]);
}
}
};

【Leetcode】【Medium】Permutations的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

  5. 【LeetCode每天一题】Permutations(排列组合)

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

  6. 【leetcode刷题笔记】Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. 【leetcode刷提笔记】Permutations

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

  8. 【LeetCode算法题库】Day7:Remove Nth Node From End of List & Valid Parentheses & Merge Two Lists

    [Q19] Given a linked list, remove the n-th node from the end of list and return its head. Example: G ...

  9. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  10. 【LeetCode算法题库】Day3:Reverse Integer & String to Integer (atoi) & Palindrome Number

    [Q7]  把数倒过来 Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Outpu ...

随机推荐

  1. Git版本回退和撤销修改

    版本回退: 在实际工作中,我们会不断对文件进行修改,然后不断提交修改到版本库里,一旦你把文件改乱了,或者误删了文件,还可以从最近的一个commit恢复,然后继续工作,而不是把几个月的工作成果全部丢失. ...

  2. windows 下创建 sqlite 数据库

    说明:windows 下执行创建 sqlite 数据库命令后数据库文件不会马上生成,需要创建表以后才会生成. 1.将 sqlite3.exe 文件放在任何位置(如放在 d:\tools )2.在 CM ...

  3. Dijkstra算法以及各种海量数据排序算法

    一.Dijkstra最短路径算法 是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止. 实现一 // // D ...

  4. (转)Heartbeat+DRBD+NFS高可用案例

    原文:http://9861015.blog.51cto.com/9851015/1939521--------------------------------Heartbeat+DRBD+NFS高可 ...

  5. Intel GPA 抓取3d模型

    原文链接在这里 http://dev.cra0kalo.com/?p=213 背景信息 Intel的GPA本身是一款图形分析软件,并没有设计从3D程序里抓取模型资源的功能,但这里作者是通过hook G ...

  6. 九度oj 1034 寻找大富翁 2009年浙江大学计算机及软件工程研究生机试真题

    题目1034:寻找大富翁 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5323 解决:2123 题目描述:     浙江桐乡乌镇共有n个人,请找出该镇上的前m个大富翁. 输入:     ...

  7. Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】

    Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. Java - 网络IO的阻塞

    最近学习时碰到事件驱动和非阻塞的相关知识,随之想到了Java中的Reactor.io与nio的一些东西:在前辈的博客上翻了翻.复习复习,在此记录一番. 实在找不到比较大点的东西,于是随便弄了个压缩包, ...

  9. spring和springboot常用注解总结

    @ConfigurationProperties 可以非常方便的把资源文件中的内容绑定到对象上   @Value("${app.name}") 注入简单值 @Import 通过导入 ...

  10. ssm框架文件上传

    有两种方法 导包和上传配置自己搞: 第一种: 上传单个文件: @RequestMapping("/addfile1") public String addfile(@Request ...