这道题是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. h5画圆

    下面一段代码是,h5的画圆,半圆,四分之一圆等效果 <!DOCTYPE html> <html lang="en"> <head> <me ...

  2. Java开发笔记(九十六)线程的基本用法

    每启动一个程序,操作系统的内存中通常会驻留该程序的一个进程,进程包含了程序的完整代码逻辑.一旦程序退出,进程也就随之结束:反之,一旦强行结束进程,程序也会跟着退出.普通的程序代码是从上往下执行的,遇到 ...

  3. Magento 0元订单 支付方式 -- Magento 0 Subtotal Payment Method

    需求 现有购物网站支持2种支付方式,但是考虑到会出现如下情况: 在一个优惠活动中,假如有一些订单的总金额为0, 那么这些订单就不必跳转到支付网关,现有支付方式无法处理此种情况. 分析 当custome ...

  4. WebService学习之旅(一)使用JAX-WS发布WebService

    JAX-WS全称Java™ API for XML Web Services,是随着JDK1.6及其后续版本发布的方便Java程序员开发WebService应用的一组API,通常简称为JWS,目前版本 ...

  5. iOS  UDP 广播 AsyncSocket 用法

    因为业务需要,需要用广播发送一个字段,在iOS开发中,用到了AsynSocket. 1.定义一个属性,负责发送和接受数据 #define YX_Local_Host @"255.255.25 ...

  6. 【R语言进行数据挖掘】回归分析

    1.线性回归 线性回归就是使用下面的预测函数预测未来观测量: 其中,x1,x2,...,xk都是预测变量(影响预测的因素),y是需要预测的目标变量(被预测变量). 线性回归模型的数据来源于澳大利亚的C ...

  7. c语言中的->代表什么意思

    c语言中 ->符号是什么意思? 比如c=a->b a为结构体或联合体的指针,->表示调用其成员

  8. map最基本操作

    #include<iostream> #include<map> using namespace std; int main() { /*map<int,char> ...

  9. Base64编码密钥时关于换行的几个问题。

    在windows下一个javaweb应用,需要用http传递公钥pk.一般是String pk = BASE64ENCODER.encode(pkBytes);base64编码时,每76个字母就要换行 ...

  10. Codeforces 727C Guess the Array

    题目传送门 长度为\(n\)的数组,询问\(n\)次来求出数组中每一个数的值 我们可以先询问三次 \(a[1]+a[2]\) \(a[1]+a[3]\) \(a[2]+a[3]\) 然后根据这三次询问 ...