// 又是可以用回溯法做的一道题。
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<int> vis(nums.size(),);
vector<vector<int>> res;
vector<int> add;
DFS(nums,,res,add,vis);
return res;
}
void DFS(vector<int>& nums,int level,vector<vector<int>>& res,vector<int>& add,vector<int>& vis){
if(level == nums.size()){res.push_back(add);return;}
else{
for(int i=;i < nums.size();i++){ //一开始不理解为啥要加个vis,其实是因为就存在前面的也要当你第一个i取中间的时候前面的也需要算这时候,每次递归都是从0 开始,就可能遍历到第一个数,所以加个vis
if(vis[i] == ){
vis[i] = ;
add.push_back(nums[i]);
DFS(nums,level+,res,add,vis);
add.pop_back();
vis[i] = ;
}
}
}
}
};
//每次start 与 i交换
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
DFS(nums,,res);
return res;
}
void DFS(vector<int>& nums,int start,vector<vector<int>>& res){
if(start == nums.size()){res.push_back(nums);return;}
else{
for(int i=start;i < nums.size();i++){
swap(nums[start],nums[i]);
DFS(nums,start+,res);
swap(nums[start],nums[i]);
}
}
}
};

LeetCode 46的更多相关文章

  1. [LeetCode] 46. Permutations 全排列

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

  2. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  3. LeetCode 46 Permutations(全排列问题)

    题目链接:https://leetcode.com/problems/permutations/?tab=Description   Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...

  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. Java实现 LeetCode 46 全排列

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

  6. [LeetCode] 46. 全排列(回溯)

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

  7. [leetcode] 46. 全排列(Java)

    46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...

  8. leetcode 46. 全排列 及 47. 全排列 II

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

  9. LeetCode(46)-Remove Nth Node From End of List

    题目: Given a linked list, remove the nth node from the end of list and return its head. For example, ...

  10. [Leetcode 46]全排列 Permutations 递归

    [题目] Given a collection of distinct integers, return all possible permutations. 数组的组合情况. Input: [1,2 ...

随机推荐

  1. Ensure Indexes Fit in RAM

    Ensure Indexes Fit in RAM — MongoDB Manual https://docs.mongodb.com/manual/tutorial/ensure-indexes-f ...

  2. OSX: 下载Flash Player的脚本

    http://blog.csdn.net/cneducation/article/details/54742983

  3. R-CNN论文详解 - CSDN博客

    废话不多说,上车吧,少年 paper链接:Rich feature hierarchies for accurate object detection and semantic segmentatio ...

  4. 从LayoutInflater分析XML布局解析成View的树形结构的过程

    上一篇博客分析了XML布局怎么载入到Activity上.不了解的能够參考 从setContentView方法分析Android载入布局流程 上一篇博客仅仅是分析了怎么讲XML布局加入到 Activit ...

  5. uchome 缓存生成

    一.uchome的缓存目录 ---------data此目录要有777权限 (1)模板文件缓存机制 1:在要显示的页面通过include template($name) 语句来包含被编译后的模板文件 ...

  6. How To Mine Bitcoins 比特币挖矿

    linux 下查看 gpu 的信息: sudo lshw -C display windows下查看cuda信息:In directory C:\Program Files\NVIDIA Corpor ...

  7. android studio 使用CMAKE

    前言 之前,每次需要边写C++代码的时候,我的内心都是拒绝的.  1. 它没有代码提示!!!这意味着我们必须自己手动敲出所有的代码,对于一个新手来说,要一个字母都不错且大小写也要正确,甚至要记得住所有 ...

  8. python实现文件夹遍历

    python 中os.path模块用于操作文件或文件夹 os.path.exists(path) 判断文件路径是否存在 dir = "c:\windows"if os.path.e ...

  9. 知识点1-树状数组[带poj Stars作为巩固]

    转自:https://blog.csdn.net/flushhip/article/details/79165701#commentBox 1.首先其中讲到了一个问题,就是如何求一个数的二进制表示的最 ...

  10. 禁止复制操作 --《C++必知必会》条款32

    class NoCopy{ private: //声明为私有的,则外部不可访问,即:不可复制 NoCopy(const NoCopy & );//复制构造函数 NoCopy & ope ...