// 又是可以用回溯法做的一道题。
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. 虚拟机中CentoOs配置ip且连网

    1.修改"VMware Network Adapter VMnet8",配置IP 2.打开虚拟机,"编辑" => "虚拟网络编辑器", ...

  2. Mysql和sqlite数据库操作心得

    经过最近一段时间的实际工作发现,原来只是认为Mysql和sqlite是分别独立的,数据传输和共享或有障碍,其实这是一个误区.当我们想要将sqlite中的数据存放到mysql中,最好的方法就是利用中间文 ...

  3. 使用Python创建.sd服务定义文件,实现脚本自动发布ArcGIS服务

    借助ArcGIS桌面发布ArcGIS服务是一个很熟悉的过程了,发布服务的前提是需要拥有一个已连接的ArcGIS Server服务站点,经过对mxd进行制图配置,定义服务参数,才能实现服务的发布,那么这 ...

  4. 如何在chrome上开启WebGL功能和判断目前浏览器是否支持

        1.开启方式: 第一种:打开cmd,切换到Chorme的安装目录,敲入chrome.exe --enable -webgl,回车就会打开一个chrome浏览器窗口: 第二种:找到Chrome浏 ...

  5. CRM - 讲师与学生

    一.讲师与学生简介 1.初始化 course_record, study_record.2.学习记录3.录入成绩4.显示成绩 ajax 查询 柱状图展示成绩 highcharts 5.上传作业(os模 ...

  6. 验证url的正则

    一. '/@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()& ...

  7. centos 配置redis

    一.配置redis 简介:Redis是使用c语言开发的一个高性能键值数据库.Redis可以通过一些键值类型来存储数据. 下载:官网地址:http://redis.io/ 下载地址:http://dow ...

  8. (2.3)DDL增强功能-流程化控制与动态sql

    1.流程控制 在T-SQL中,与流程控制语句相关的关键字有8个: BEGIN...END BREAK GOTO CONTINUE IF...ELSE WHILE RETURN WAITFOR 其实还可 ...

  9. 使用Rxjava自己创建RxBus

    https://piercezaifman.com/how-to-make-an-event-bus-with-rxjava-and-rxandroid/ https://lingyunzhu.git ...

  10. PAT 1053 Path of Equal Weight[比较]

    1053 Path of Equal Weight(30 分) Given a non-empty tree with root R, and with weight W​i​​ assigned t ...