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

两种方法,第一种用了STL中的函数,第二种用递归+回溯,我个人很喜欢第二种方法

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//vector<vector<int>> permute(vector<int>& nums) {
// sort(nums.begin(), nums.end());
// vector<vector<int>> a;
// do {
// a.push_back(nums);
// } while (std::next_permutation(nums.begin(), nums.end()));
// return a;
//} void backtracking(vector<int>& nums,int start,vector<int> &temp,vector<vector<int>> &ans) { if (!nums.size()) return;
if (start>=nums.size())
{
ans.push_back(temp);
return;
}
for (int i=start; i<nums.size();i++)
{
swap(nums[i],nums[start]);
temp.push_back(nums[start]);
backtracking(nums, start+, temp,ans);// 递归求解
temp.pop_back();//回溯,不影响此次的循环
swap(nums[i], nums[start]);//回溯不影响此次的循环
}
}
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> ans;
vector<int> curSeq;
backtracking(nums, , curSeq,ans);
return ans;
} int main() {
vector<int> nums={,,};
int ans=permute(nums).size();
std::cout << ans << std::endl;
return ;
}

#leetcode刷题之路46-全排列的更多相关文章

  1. python -- leetcode 刷题之路

    第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...

  2. 使用Java+Kotlin双语言的LeetCode刷题之路(三)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  3. 使用Java+Kotlin双语言的LeetCode刷题之路(二)

    BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...

  4. 使用Java+Kotlin双语言的LeetCode刷题之路(一)

    LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...

  5. #leetcode刷题之路40-组合总和 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...

  6. #leetcode刷题之路47-全排列 II

    给定一个可包含重复数字的序列,返回所有不重复的全排列.示例:输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 之前的https://www.cnblogs.com/ ...

  7. #leetcode刷题之路16-最接近的三数之和

    给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...

  8. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  9. #leetcode刷题之路6- Z 字形变换

    将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L     C     I ...

随机推荐

  1. webpack的require是如何工作的?

    https://stackoverflow.com/questions/35625593/how-does-webpacks-require-work wepback的require函数调用类似于no ...

  2. centos7.4 nfs-2.3.2

    http://www.linuxfromscratch.org/blfs/view/svn/basicnet/libtirpc.html 注释:安装环境centos7.4;   安装完软件成后会升级系 ...

  3. 一、MySQL中的索引 二、MySQL中的函数 三、MySQL数据库的备份和恢复 四、数据库设计和优化(重点)

    一.MySQL中的索引###<1>索引的概念 索引就是一种数据结构(高效获取数据),在mysql中以文件的方式存在.存储建立了索引列的地址或者指向. 文件 :(以某种数据 结构存放) 存放 ...

  4. tomcat-7.0.30安装及配置

    使用说明: 一.1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),2.安装jdk-6u14-windows-i586.exe 3.配置环境 ...

  5. 在windows下的hdfs客户端编写

    在windows下的hdfs客户端编写 新建一个工程,右键 properties -> java build path -> libraries 和之前一样的操作,这次 new 一个 us ...

  6. 张高兴的 Windows 10 IoT 开发笔记:使用 MAX7219 驱动数码管

    This is a Windows 10 IoT Core project on the Raspberry Pi 2/3, coded by C#. GitHub:https://github.co ...

  7. HBase搭建部署

    基础环境准备 zookeeper集群(参开地址:https://www.cnblogs.com/starzy/p/10376642.html) Hadoop集群(参考地址:https://www.cn ...

  8. [T-ARA][yayaya]

    歌词来源:http://music.163.com/#/song?id=22704449 U look at me Right T-ARA U Ready Let me seeya LaLaLaLa ...

  9. JVM源码分析之堆外内存完全解读

    JVM源码分析之堆外内存完全解读   寒泉子 2016-01-15 17:26:16 浏览6837 评论0 阿里技术协会 摘要: 概述 广义的堆外内存 说到堆外内存,那大家肯定想到堆内内存,这也是我们 ...

  10. GIT非常见命令使用笔记

    1:修改已经提交N次代码的user.name和user.email 解决我在多电脑间,使用不同账户,git config 的global,system,local配置忽略改动,而添加了多台电脑ssh ...