【Leetcode】【Medium】Permutations
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的更多相关文章
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- 【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 ...
- ACM金牌选手整理的【LeetCode刷题顺序】
算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...
- 【LeetCode每天一题】Permutations(排列组合)
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- 【leetcode刷题笔记】Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【leetcode刷提笔记】Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- Java - 尚学堂第八章常用类(将输入的string类型的值转为整数、浮点型、日期类型)
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; imp ...
- eureka 和zookeeper 区别 优势
作为服务注册中心,Eureka比Zookeeper好在哪里 著名的CAP理论指出,一个分布式系统不可能同时满足C(一致性).A(可用性)和P(分区容错性).由于分区容错性在是分布式系统中必须要保证的, ...
- 【转】常用算法复习及实现(C++版)
一.霍夫曼树实现 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大 ...
- C# CultureInfo中常用的InvariantCulture
本文参考自CultureInfo中重要的InvariantCulture,纯属读书笔记,加深记忆 1.CultureInfo的InvariantCulture的作用 (1).CultureInfo使整 ...
- selenium+Python(表单、多窗口切换)
1.多表单切换 在Web应用中经常会遇到frame/iframe表单嵌套页面的应用,WebDriver只能在一个页面上对元素识别与定位,对于frame/iframe表单内嵌页面上的元素无法直接定位.这 ...
- ambari-server启动出现ERROR main] DBAccessorImpl:106 - Error while creating database accessor java.lang.ClassNotFoundException:com.mysql.jdbc.Driver问题解决办法(图文详解)
不多说,直接上干货! 问题详情 ambari-server启动时,报如下的错误 问题分析 注:启动ambari访问前,请确保mysql驱动已经放置在/usr/share/Java内且名字是mysql- ...
- c++ 网络编程(一)TCP/UDP windows/linux 下入门级socket通信 客户端与服务端交互代码
原文作者:aircraft 原文地址:https://www.cnblogs.com/DOMLX/p/9601511.html c++ 网络编程(一)TCP/UDP 入门级客户端与服务端交互代码 网 ...
- 将Mysql的一张表导出至Excel格式文件
将Mysql的一张表导出至Excel格式文件 导出语句 进入mysql数据库,输入如下sql语句: select id, name, age from tablename into outfile ' ...
- spring自定义标签之 规范定义XSD
引言: spring的配置文件中,一切的标签都是spring定义好的.<bean/>等等,有了定义的规范,才能让用户填写的正常可用.想写自定义标签,但首先需要了解XML Schema De ...
- XAML语法及标记扩展、附加属性、特定的字符和空白
1.对象元素语法 使用开闭标签将对象定义一个XML元素,这种语法与其他标记语言如HTML的元素语法非常相似,在以左右尖括号保卫要设置的类或结构的类型名称.对象元素可以声明0个或多个属性,以一个或多个空 ...