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

做了几个 backtrack 类的题目了, 对这个题还是没思路.



我目前的经验, backtrack 题目,要确定下面三个重要部件:

  1. 把 temp 压入 res 的条件!

    • 通常这个样 if (condition) {res.push_back(temp);}
  2. 怎样生成符合题意的 temp!
    • 通常这个样 else{for(int i=var; i<A.size(); i++){temp.push_back(A[i]); backtrack(params); temp.pop_back();}}
  3. backtrack 函数参数列表.

上面三点考虑时似乎没个先后顺序,好难哦.

人家想法,自个代码(被教育后的结果):

vector<vector<int>> permute(vector<int>& A) {
vector < vector<int> > res;
vector<int> temp;
backtrack(A, res, temp);
return res;
} void backtrack(vector<int>& A, vector<vector<int> >& res, vector<int> temp) {
// 重要部件
if (temp.size() == A.size()) {
res.push_back(temp); // 1. 啥时候 把 temp 压入 res, 很重要!!
return;
} else {
// 2. 如何生成 temp 很重要!!
for (int i = 0; i < A.size(); i++) {
// contain A[i] is true
if (find(temp.begin(), temp.end(), A[i]) != temp.end())
continue; temp.push_back(A[i]);
backtrack(A, res, temp);
temp.pop_back();
}
return;
}
}

46. Permutations(medium, backtrack, 重要)的更多相关文章

  1. 刷题46. Permutations

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

  2. LeetCode - 46. Permutations

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

  3. [Leetcode][Python]46: Permutations

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

  4. 46. Permutations 排列数

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

  5. 47. Permutations II(medium, backtrack, 重要, 条件较难思考)

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

  6. 46. Permutations (Back-Track,Sort)

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

  7. LeetCode:46. Permutations(Medium)

    1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...

  8. 46 Permutations(全排列Medium)

    题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...

  9. [LeetCode] 46. Permutations 全排列

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

随机推荐

  1. java 中文乱码问题,请注意response.getWriter的顺序

    反例: 正例:

  2. HTML-----<a>、<table>、<form>解析

      超链接 anchor 锚 <a href="url">内容</a> Href Hypertext reference 引用 URL(Uniform Re ...

  3. Python/ selectors模块及队列

    Python/selectors模块及队列 selectors模块是可以实现IO多路复用机制: 它具有根据平台选出最佳的IO多路机制,比如在win的系统上他默认的是select模式而在linux上它默 ...

  4. 如何从二维数组中的多个key中获取指定key的值?

    精华 LOVEME96 2016-10-21 10:40:19 浏览(1512) 回答(3) 赞(0) 新手求教:二维数组中一般会有多个key,如果我们要获得指定key的值,应该怎么做? 问题标签: ...

  5. Spring Cloud学习笔记-002

    搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...

  6. 调用Kubernetes API操作Kubernetes

    准备工作 首先要准备一个1.5+版本的Kubernetes,并且开放了API Server的http访问端口8080.本文使用的是1.10的版本,没有环境的可以参考我上一篇文章<在CentOS ...

  7. getgpc($k, $t='GP'),怎么返回的是 NULL?

    <?php /** * 实用小代码 * 获得GET POST COOKIS */ $html=<<<WORD <form method="post"& ...

  8. Field的getModifiers()方法返回int类型值表示该字段的修饰符

    其中,该修饰符是java.lang.reflect.Modifier的静态属性. 对应表如下: PUBLIC: 1PRIVATE: 2PROTECTED: 4STATIC: 8FINAL: 16SYN ...

  9. nginx方向代理

    nginx 的安装 # yum install nginx 新建配置文件 # vi /etc/nginx/conf.d/resume-xyz-8081.conf 配置 upstream resume ...

  10. [AHOI 2009]chess 中国象棋

    Description 题库链接 给你一张 \(N\times M\) 的棋盘.要求每行每列最多放两个棋子,问总方案数. \(1\leq N,M\leq 100\) Solution 记 \(f_{i ...