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. webpack打包性能优化

    1. 使用 gzip 压缩打包后的 js 文件 这个方法优化浏览器下载时的文件大小(打包后的文件大小没有改变) webpack.config.prod.js 中 var CompressionWebp ...

  2. pthon/零起点(一、集合)

    pthon/零起点(一.集合) set( )集合,集合是无序的,集合是可变的,集合是可迭代的 set()强型转成集合数据类型 set()集合本身就是去掉重复的元素 集合更新操作案列: j={1,2,3 ...

  3. python 评论楼

    评论楼 从数据库中取出本篇博客的所有评论使用python语句将评论整理成具有层级关系的列表 typename=request.POST.get('typename') comment_list = m ...

  4. Spark测试代码

    测试代码: import org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.sql.hive.HiveContext ...

  5. assert后面如果是假则程序崩溃

    assert后面如果是假,则程序崩溃.

  6. jacascript DOM节点——节点内容

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! innerHTML 在读模式下,返回与调用元素的所有子节点(包括元素.注释和文本节点)对应的 HTML 标 ...

  7. 自行实现高性能MVC WebAPI

    wcf虽然功能多.扩展性强但是也面临配置忒多,而且restful的功能相当怪异,并且目前没法移植.asp.net core虽然支持webapi,但是功能也相对繁多.配置复杂.就没有一个能让码农们安安心 ...

  8. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  9. Headless Chrome:服务端渲染JS站点的一个方案【上篇】【翻译】

    原文链接:https://developers.google.com/web/tools/puppeteer/articles/ssr 注:由于英文水平有限,没有逐字翻译,可以选择直接阅读原文 tip ...

  10. github的简单使用

    查了好多入门教程(图文并茂可以了解一些基本步骤),感觉逻辑欠缺,(很多东西跟着教程了解会用了,不了解逻辑,只是会了这一个,其他的还是很蒙),来一起理一理把 1.第一步下载并注册(这个自己解决) 2.用 ...