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]
]

题目大意:求一个序列的全排列。

思路:做排列组合时,模拟该过程得到思路。首先选择集合中的某一个元素作为第一位,接着在剩下的元素中选择一个元素作为第二位,一直到为空。
上述过程显然是个递归的过程,声明帮助函数:Helper(vector<vector<int> >& result, vector<int>& tmp, vector<int>& nums)。tmp用来存储已经选定的元素,nums用来存储剩下的元素,result存储所得的情况。显然当nums为空的时候递归应该返回。
注意:关键在于对nums和tmp的处理,一旦tmp中增加了一个元素,nums就要删除该元素;每次有一个帮助函数返回时都会进行一次回溯,即nums要插入元素,tmp要删除元素。

代码如下:(采用典型的回溯来处理)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
    void permuteHelper(vector<vector<int> >& result,vector<int> &tmp,vector<int>& nums)
    {
        if(nums.empty())
        {
            result.push_back(tmp);
            return;
        }
         
        for(int i = 0; i < nums.size(); i++)
        {
            int val = nums[i];
            tmp.push_back(val);
            nums.erase(nums.begin()+i);
            permuteHelper(result,tmp,nums);
            nums.insert(nums.begin()+i,val);
            tmp.pop_back();
        }
    }
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> result;
        vector<int> tmp;
        permuteHelper(result,tmp,nums);
         
        return result;
    }
};

2016-08-07 15:11:04

本文转自313119992 51CTO博客,原文链接:http://blog.51cto.com/qiaopeng688/1835352

leetCode 46. Permutations 回溯问题 | Medium相关推荐

  1. 【数字全排列】LeetCode 46. Permutations

    LeetCode 46. Permutations Solution0: 补充一个偷鸡摸狗的方法.偷懒的做法直接使用std::next_permutation()函数 class Solution { ...

  2. LeetCode 46. Permutations

    46. Permutations Given a collection of distinct numbers, return all possible permutations. For examp ...

  3. 135. Leetcode 46. 全排列 (回溯算法-排列问题)

    class Solution:def permute(self, nums: List[int]) -> List[List[int]]:# 方法一# res = [] # 存放符合条件结果的集 ...

  4. LeetCode算法总结-回溯法与深度优先搜索

    转载自  LeetCode算法总结-回溯法与深度优先搜索 回溯法(探索与回溯法)是一种选优搜索法,又称为试探法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退 ...

  5. 【数字全排列】LeetCode 47. Permutations II

    LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...

  6. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations.For exa ...

  7. java回溯算法_回溯算法讲解--适用于leetcode绝大多数回溯题目

    什么是回溯算法? 回溯法是一种系统搜索问题解空间的方法.为了实现回溯,需要给问题定义一个解空间. 说到底它是一种搜索算法.只是这里的搜索是在一个叫做解空间的地方搜索. 而往往所谓的dfs,bfs都是在 ...

  8. 【LeetCode】163.Missing Ranges(Medium)(带锁题)解题报告

    [LeetCode]163.Missing Ranges(Medium)(带锁题)解题报告 题目地址:https://leetcode.com/problems/missing-ranges/(带锁题 ...

  9. LeetCode 46. 全排列(回溯)

    文章目录 1. 题目信息 2. 解题 2.1 利用hash map解决 2.2 改用bool数组判断是否出现过 1. 题目信息 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例:输入: [1 ...

最新文章

  1. Code Reading -chap4
  2. Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名重定向
  3. spingboot实现redis的发布订阅
  4. metadata usage in the runtime
  5. UILabel 根据text的内容来调整大小
  6. python自定义colorbar_python可视化 matplotlib画图使用colorbar工具自定义颜色
  7. 无线数传在桥梁检测中传感器信号的采集应用
  8. android版本如何修改时间,如何修改Android系统默认时间
  9. Delphi Format函数功能及用法详解
  10. 字节跳动最爱考的前端面试题:计算机网络基础
  11. 【es】ElasticSearch master 选举
  12. Python标准库-string模块《未完待续》
  13. 注册表知识and技巧大全
  14. CentOS7.6新增或修改SSH端口号的步骤
  15. OSChina 周六乱弹 —— 假如你被熊困到树上
  16. 2021第六届数维杯大学生数学建模竞赛题目
  17. tabbar模板html,新闻手机模板,html模板,小程序模板,App模板
  18. 5G行业消费者洞察:这23个词最热
  19. 开发的激光测距仪PCBA方案设计
  20. 工作中如何做好技术积累『转载-保持学习的空杯心态』

热门文章

  1. 小型企业组织建设第二阶段
  2. 常用[js,css,jquery,html]
  3. jfreechart的使用
  4. 用bitmap实现中位数的算法
  5. 如何来理解Python中的字典数据类型
  6. springboot 请求路径有后缀_springboot指定访问url接口后缀:*.do或*.action
  7. k8s-harbor安装
  8. 单片机如何使用?单片机只会用例程怎么办?
  9. android启动流程之lk,Android系统之LK启动流程分析(一)
  10. 批量处理word文件内容_用python批量提取word文件信息,导出到excel文件