这道题是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(全排列)的更多相关文章

  1. [LeetCode] Permutations 全排列

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

  2. leetcode Permutations II 无重全排列

    作者:jostree  转载请注明出处 http://www.cnblogs.com/jostree/p/4051169.html 题目链接:leetcode Permutations II 无重全排 ...

  3. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  4. 每日一题-——LeetCode(46)全排列

    题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...

  5. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  6. LeetCode:全排列【46】

    LeetCode:全排列[46] 题目描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2 ...

  7. LeetCode 47——全排列 II

    1. 题目 2. 解答 在 LeetCode 46--全排列 中我们已经知道,全排列其实就是先确定某一个位置的元素,然后余下就是一个子问题.在那个问题中,数据没有重复,所以数据中的任意元素都可以放在最 ...

  8. [LeetCode] Permutations II 全排列之二

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

  9. [LeetCode] 46. Permutations 全排列

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

  10. [leetcode]46. Permutations全排列(给定序列无重复元素)

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

随机推荐

  1. 创建对象js.

    JavaScript中的基本书记类型. Number(数值类型) String(字符串类型) boolean(布尔类型) null(空类型) undefined(未定义类型) object 常见的内置 ...

  2. FastDFS java 辅助类

    package cn.saiz.drkms.task.crack.utils; import java.io.File; import java.io.FileInputStream; import ...

  3. AJPFX关于VIM的常用快捷键

    Ajax技术的核心是XMLHttpRequest对象(简称XHR),var xhr = new XMLHttpRequest();function createXHR(){if (typeof XML ...

  4. Spring Cloud Gateway VS Zuul 比较,怎么选择?

    Spring Cloud Gateway 是 Spring Cloud Finchley 版推出来的新组件,用来代替服务网关:Zuul. 那 Spring Cloud Gateway 和 Zuul 都 ...

  5. uvm_monitor——借我一双慧眼

    monitor 用来捕获(监视)和检查总线的信号是否满足预期的要求.所有的user_monitor 继承自uvm_monitor,uvm_monitor继承自uvm_component,从源代码来看里 ...

  6. iOS Block的本质(一)

    iOS Block的本质(一) 1.对block有一个基本的认识 block本质上也是一个oc对象,他内部也有一个isa指针.block是封装了函数调用以及函数调用环境的OC对象. 2.探寻block ...

  7. IOS7 Text View 截断的问题解决

    - (void)textViewDidChange:(UITextView *)textView { CGRect line = [textView caretRectForPosition: tex ...

  8. 特别困的学生 UVa12108(模拟题)

    一.题目 课堂上有n个学生(n<=10).每个学生都有一个“睡眠-清醒”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复(1<=Ai,Bi<=5),初始第i个同学处于他的周期的C ...

  9. python之路——目录

    目录 python基础部分 基础部分 介绍.基本语法.流程控制 列表 元祖 字符串 字典 集合 文件操作 函数 变量 递归 迭代器,生成器,装饰器,Json和pickle 数据序列化 函数 初识函数 ...

  10. Java形式参数和返回值的问题

    形式参数和返回值的问题 (1).形式参数: A.类名:需要该类的对象. B.抽象类名:需要该类的子类对象. C.接口名:需要该接口的实现类对象. A.类名作为形式参数 class Student { ...