【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题。
题目要求:
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
回溯法解题,函数的递归调用。
提交代码:
class Solution {
public:
void getsol(vector<int>& nums, int a[], int k, vector<int>& sol, vector<vector<int>>& res) {
for (int i = 0; i < nums.size(); i++) {
if (a[i]) {
a[i] = 0;
sol.push_back(nums[i]);
getsol(nums, a, k + 1, sol, res);
sol.pop_back();
a[i] = 1;
}
if (k == nums.size()) {
res.push_back(sol);
return;
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>>res;
vector<int>sol;
int *a = new int[nums.size()];//这个数组用来记录该数组是否在sol中,避免重复
for (int i = 0; i < nums.size(); i++)a[i] = 1;
for (int i = 0; i < nums.size(); i++) {
a[i] = 0;
sol.push_back(nums[i]);
getsol(nums, a, 1, sol, res);
sol.pop_back();
a[i] = 1;
}
return res;
}
};
提交结果:

个人总结:
回溯法的题目还是需要多多调试熟悉一下它的运行过程,堆栈的调用和转移。
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
if(nums.empty())return vector<vector<int>>(1,vector<int>());
vector<vector<int>>res;
int first=nums[0];
nums.erase(nums.begin());
vector<vector<int>>words=permute(nums);
for(auto &a:words) {
for(int i=0;i<=a.size();++i) {
a.insert(a.begin()+i,first);
res.push_back(a);
a.erase(a.begin()+i);
}
}
return res;
}
};

像这种方法是在把从下一层返回的 res 赋予 words,然后使用 a 容器遍历 words,把 get 到的数 first 依次插入到容器 a 里在放入 res 中保存在把插入的 first 取出,然后在把所得的结果一步一步以 res 的形式返回给上一层。
【LeetCode】Permutations(全排列)的更多相关文章
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- leetcode Permutations II 无重全排列
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...
- [CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- LeetCode:全排列II【47】
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
- LeetCode:全排列【46】
LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...
- LeetCode 47——全排列 II
1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
随机推荐
- 分享一套Code Smith 搭建N层架构模板
开篇 平常开发时,由于冗余代码过多,程序员做重复的工作过多势必会影响开发效率.倘若 对重复性代码简单的复制.粘贴,虽然也能节省时间,但也需仔细一步步替换,这无疑也是一件费力的事.这时我们急需代码生成工 ...
- Spring AOP初步总结(一)
学习AOP有段时间了,一直没空总结一下,导致有些知识点都遗忘了,之后会把以前学过的Spring核心相关的知识点总结一轮... 先大体介绍下Spring AOP的特点(均摘自"Spring i ...
- CF1023D Array Restoration
思路: 使用set即可,细节很多,容易出错. 实现: #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3 ...
- JSP serverlet区别与联系
jsp是html包含java servlet是java包含html jsp请求到tomcat---tomcat封装了jsp到servlet实现. 所以jsp请求时候,会自动创建session 而不用在 ...
- 验证 .NET 4.6 的 SIMD 硬件加速支持的重要性
SIMD 的意思是 Single Instruction Multiple Data.顾名思义,一个指令可以处理多个数据. .NET Framework 4.6 推出的 Nuget 程序包 Syste ...
- WPF中做出一个QQ登陆界面
Xaml: <Window x:Class="ChatSoftware.MainWindow" xmlns="http://schemas.microsoft.co ...
- 为什么JS是单线程?JS中的Event Loop(事件循环)?JS如何实现异步?setimeout?
https://segmentfault.com/a/1190000012806637 https://www.jianshu.com/p/93d756db8c81 首先,请牢记2点: (1) JS是 ...
- OpenRead方法打开文件并读取
实现效果: 知识运用: File类的OpenRead方法 //实现打开现有文件以进行读取 public static FileStream OpenRead(string path) FileStre ...
- PyCharm如何配置断点调试功能
1. 点击菜单 PyCharm -> Preferences.. 2. 在左侧菜单栏找到Project:Django - > Project Interpreter 并点击配置 Proje ...
- javascript“类”与继承总结和回顾
Javascipt语法不支持"类"(class)[es6已经支持],但是有模拟类的方法.今天我主要谈谈Javascipt中模拟“类”的方法及js中继承的总结和回顾. js中实现“类 ...