46. Permutations

题目

 Given a collection of distinct numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations: [
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

解析

class Solution_46 {
public:
void help(int i,vector<int> &nums,vector<vector<int>> &vecs)
{ if (i==nums.size())
{
vecs.push_back(nums);
return;
}
else
{
for (int j = i; j < nums.size();j++)
{
swap(nums[i],nums[j]);
help(i + 1, nums,vecs);
swap(nums[i],nums[j]);
}
}
return;
} vector<vector<int>> permute(vector<int>& nums) { vector<vector<int>> vecs; if (nums.size()==0)
{
return vecs;
} help(0, nums,vecs); return vecs;
}
};

题目来源

46. Permutations 排列数的更多相关文章

  1. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  2. SCU 4424(求子集排列数)

    A - A Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice ...

  3. 【一天一道LeetCode】#46. Permutations

    一天一道LeetCode系列 (一)题目 Given a collection of distinct numbers, return all possible permutations. For e ...

  4. 31. Next Permutation + 46. Permutations + 47. Permutations II + 60. Permutation Sequence

    ▶ 问题:字典序生成有关的问题. ▶ 31. 由当前序列生成字典序里的下一个序列. ● 初版代码,19 ms class Solution { public: void nextPermutation ...

  5. LeetCode - 46. Permutations

    46. Permutations Problem's Link -------------------------------------------------------------------- ...

  6. 由abcd四个字符取5个作允许重复的排列,要求a出现次数不超过2次,但不能不出现;b不超过1个;c不超过3个;d出现的次数为偶数。求满足以上条件的排列数。

    一.我的解法       由于没复习,我在想一般的方法,那就是d取0.2.4,然后分步计算,得到225这个错误答案. 二.指数型母函数       设满足以上条件取个排列的排列数为,的指数型母函数为 ...

  7. [Leetcode][Python]46: Permutations

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...

  8. 乘法原理,加法原理,多重集的排列数(多个系列操作穿插的排列数) 进阶指南 洛谷p4778

    https://www.luogu.org/problemnew/solution/P4778 非常好的题目,囊括了乘法加法原理和多重集合排列,虽然最后使用一个结论解出来的.. 给定一个n的排列,用最 ...

  9. 排列数与For的关系

    目录 什么是排列数 用现实模型表示 用Python编程表示 用数学符号表示 规律 规律1 规律2 如果m < n 会怎样? 排列数的应用场景 什么是排列数 排列指将一个集合里的每个元素不重复的排 ...

随机推荐

  1. linux下面某些常用命令的用法【转】

    转自:http://blog.csdn.net/luo3532869/article/details/7584290 ls 命令用于常看目录,用法:ls [选项][目录或文件]例:使用ls命令显示/h ...

  2. input button 不能在后台用Enabled

    <input type="button" value="上传" class="uploadButton"  runat="s ...

  3. Spring+Junit,测试事务时,一直回滚

    研究了好长时间,都不知道原因,也不能解决. 控制台又没有报异常,但是就是一直回滚.注释掉spring声明式配置的代码,就能正确的更新数据. 从网上看了,别人的文章 http://blog.csdn.n ...

  4. 关于transform的属性

    transfrom的出现让许多静态的东西动了起来,让网页更加具有生命力. 在transform属性中,transform-origin属性仅是其中之一,要彻底理解transform属性,这是不够的,必 ...

  5. 【SQL】事务

    1.事务的开始结束: START TRANSACTION :标记事务开始 COMMIT :标记事务成功结束 ROLLBACK :标记事务夭折 2.设定事务只读.读写性质: SET TRANSACTIO ...

  6. python的property的用法

    假设定义了一个类:C,该类必须继承自object类,有一私有变量_xclass C: def __init__(self): self.__x=None 1.现在介绍第一种使用属性的方法: 在该类中定 ...

  7. redis发布订阅、HyperLogLog与GEO功能的介绍

    一.发布订阅 1.模型 发布者发布消息,订阅者接收消息 2.API 2.1.publish 2.2.订阅 2.3.取消订阅 unsubsribe 2.4.其他api 二.HyperLogLog 极小空 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统

    http://www.tuicool.com/articles/NfyqQr 本节主要知识点是easyui 的手风琴加树结构做菜单导航 有园友抱怨原来菜单非常难看,但是基于原有树形无限级别的设计,没有 ...

  9. hadoop学习二:hadoop基本架构与shell操作

    1.hadoop1.0与hadoop2.0的区别:

  10. Python3 数字

    layout: post title: Python3 数字 author: "luowentaoaa" catalog: true tags: mathjax: true Pyt ...