给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ]

class Solution {
public:
vector<vector<int> >res;
vector<int> visit;
int size;
vector<vector<int> > permute(vector<int>& nums)
{
int len = nums.size();
if(len == 0)
return res;
size = len;
visit = vector<int>(len, 0);
vector<int> temp;
DFS(nums, temp, 0);
return res;
} void DFS(vector<int>& nums, vector<int> &temp, int len)
{
if(len == size)
{
res.push_back(temp);
}
for(int i = 0; i < size; i++)
{
if(visit[i] == 1)
continue;
visit[i] = 1;
temp.push_back(nums[i]);
DFS(nums, temp, len + 1);
visit[i] = 0;
temp.pop_back();
}
}
};

Leetcode46. Permutations全排列的更多相关文章

  1. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  2. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  3. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  4. [leetcode]46. Permutations全排列(给定序列无重复元素)

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

  5. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 46. Permutations (全排列)

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  7. LeetCode46. Permutations

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

  8. 046 Permutations 全排列

    给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2 ...

  9. [LeetCode] 46. Permutations 全排列

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

随机推荐

  1. 深入浅出 Java Concurrency (40): 并发总结 part 4 性能与伸缩性[转]

    性能与伸缩性 使用线程的一种说法是为了提高性能.多线程可以使程序充分利用闲置的资源,提高资源的利用率,同时能够并行处理任务,提高系统的响应性. 但是很显然,引入线程的同时也引入了系统的复杂性.另外系统 ...

  2. vue-cli 本地环境 API 代理设置和解决跨域

    前言 我们在使用 vue-cli 启动项目的时候npm run dev便可以启动我们的项目了,通常我们的请求地址是以 localhost: 来请求接口数据的,localhost 是没有办法设置 coo ...

  3. keil mdk 无法添加对应容量的芯片

    如果包已经安装好了 贴到 回到mdk,完事儿

  4. Leetcode946. Validate Stack Sequences验证栈序列

    给定 pushed 和 popped 两个序列,只有当它们可能是在最初空栈上进行的推入 push 和弹出 pop 操作序列的结果时,返回 true:否则,返回 false . 示例 1: 输入:pus ...

  5. set_case_analysis

    set_case_analysis给某个Pin指定固定电平,然后时序分析会根据该结果优化原先的电路. 试举例如下:

  6. CF1140F - Extending Set of Points

    题意:对于点集S,定义函数F(S)为对S不断扩展到不能扩展时S的点数.一次扩展定义为如果有一个平行于坐标轴的矩形的三个点在S中,则第四个点加入S. 动态在S中加点删点,每次操作完后求F(S)的值. 解 ...

  7. HZOI20190822模拟29题解

    题面:https://www.cnblogs.com/Juve/articles/11396238.html 下面开始一句话题解: A:爬山: 二分答案,check即可 #include<ios ...

  8. #pragma omp parallel for

    #pragma omp parallel for是OpenMP中的一个指令,表示接下来的for循环将被多线程执行,另外每次循环之间不能有关系.示例如下: int main(int argc, char ...

  9. PAT甲级——A1029 Median

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  10. mysql利用MySQLWorkbench生成数据表之间的关系图

    先看结果,默认是展开的,我手动把表折叠了 那么如何实现呢 先点击这里 然后通过向导来创建即可,一直到finish就行了