// 又是可以用回溯法做的一道题。
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. 【抓包】火狐浏览器F12

    页面请求服务器的get.post两种请求,还有其他种,但是其他中基本不用,所以只记住get和post两种请求方法即可. 1.get(当前页面向服务器传值--即请求服务器)---弊端--传值长度有限 F ...

  2. 第一个Shader程序

    fx文件: float4x4 matWorld; float Time=1.0f; struct VS_OUTPUT { float4 Pos :POSITION; float4 Color :COL ...

  3. Django之logging日志使用

    Logger模块 是python中用于便捷记录日志且线程安全的模块 使用logging模块记录日志涉及四个主要类: logger提供了应用程序可以直接使用的接口: handler将(logger创建的 ...

  4. [css]单/多行居中&字体设置

    行高和字号 行高 CSS中,所有的行,都有行高.盒模型的padding,绝对不是直接作用在文字上的,而是作用在"行"上的. line-height: 40px; 文字,是在自己的行 ...

  5. jsoup做http接口测试

    本文转载张飞的博客http://www.cnblogs.com/zhangfei/p/4359408.html在此感谢博主的分享! 最早用Jsoup是有一个小的爬虫应用要写,发现Jsoup较HttpC ...

  6. php \r \n 和 <br/> \t

    利用\r \n 和 <br/> \t做了个实验,话不多说,看代码就很清楚的知道

  7. capistranorb

    远程服务器自动部署工具 https://capistranorb.com/

  8. 2018 Multi-University Training Contest 8 Solution

    A - Character Encoding 题意:用m个$0-n-1$的数去构成k,求方案数 思路:当没有0-n-1这个条件是答案为C(k+m-1, m-1),减去有大于的关于n的情况,当有i个n时 ...

  9. ACM-ICPC 2018 沈阳赛区网络预赛 Solution

    A. Gudako and Ritsuka 留坑. B. Call of Accepted 题意:定义了一种新的运算符$x d y$ 然后给出中缀表达式,求值 思路:先中缀转后缀,然后考虑如何最大如何 ...

  10. Python笔记 #10# Histograms

    1.Build a histogram In [1]: help(plt.hist) Help on function hist in module matplotlib.pyplot: hist(x ...