LeetCode 46
// 又是可以用回溯法做的一道题。
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的更多相关文章
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- LeetCode 46 Permutations(全排列问题)
题目链接:https://leetcode.com/problems/permutations/?tab=Description Problem:给出一个数组(数组中的元素均不相同),求出这个数组 ...
- 每日一题-——LeetCode(46)全排列
题目描述: 给定一个没有重复数字的序列,返回其所有可能的全排列.输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ...
- Java实现 LeetCode 46 全排列
46. 全排列 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2] ...
- [LeetCode] 46. 全排列(回溯)
###题目 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], ...
- [leetcode] 46. 全排列(Java)
46. 全排列 这题我们可以借用31. 下一个排列写的nextPermutation函数来做,稍微改造一下即可 注意要先给nums排个序 class Solution { // 当没有下一个排列时re ...
- leetcode 46. 全排列 及 47. 全排列 II
46. 全排列 问题描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3 ...
- 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, ...
- [Leetcode 46]全排列 Permutations 递归
[题目] Given a collection of distinct integers, return all possible permutations. 数组的组合情况. Input: [1,2 ...
随机推荐
- try...finally的妙用
受博文 C#中Finally的一个不太常见的用法 的启发,正好在开发中遇到这样一段代码: public bool ChangeBlogApp(Guid userID, string oldBlogAp ...
- POI官网中的例子
官方指南中的例子: http://poi.apache.org/spreadsheet/quick-guide.html#New+Sheet 这一节 Workbook wb = new HSSFWor ...
- Chinese_remainder_theorem
https://en.wikipedia.org/wiki/Chinese_remainder_theorem 中国剩余定理 https://en.wikipedia.org/wiki/RSA_(cr ...
- numpy基本方法总结 --good
https://www.cnblogs.com/xinchrome/p/5043480.html 一.数组方法 创建数组:arange()创建一维数组:array()创建一维或多维数组,其参数是类似于 ...
- Linux常用命令【总结】
Linux命令中文版详解:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/index.html Linux系统,我用过的有centos和 ...
- hdfs HA原理
早期的hadoop版本,NN是HDFS集群的单点故障点,每一个集群只有一个NN,如果这个机器或进程不可用,整个集群就无法使用.为了解决这个问题,出现了一堆针对HDFS HA的解决方案(如:Linux ...
- 5.8 Components — Composing Components(组合组件)
一.概述 当你通过和另外一个组件组合的时候,组件就会真正发挥它们的所有潜能.比如<ul>元素,只有<li>元素是适合作为它的子元素的.如果我们希望同样类型的行为,那么我们就必须 ...
- Linux内核参数之arp_ignore和arp_announce
一.arp_ignore和arp_announce介绍 arp_ignore和arp_announce参数都和ARP协议相关,主要用于控制系统返回arp响应和发送arp请求时的动作.这两个参数很重要, ...
- 常用php操作redis命令整理(二)哈希类型
HSET将哈希表key中的域field的值设为value;如果field是哈希表中的一个新建域,并且值设置成功,返回1;如果哈希表中域field已经存在且旧值已被新值覆盖,返回0. <?php ...
- .net Core 发布并布署到Iis
配置 Program.cs代码 namespace WebApplication8 { public class Program { public static void Mai ...