46. Permutations(medium, backtrack, 重要)
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 题目,要确定下面三个重要部件:
- 把 temp 压入 res 的条件!
- 通常这个样
if (condition) {res.push_back(temp);}
- 通常这个样
- 怎样生成符合题意的 temp!
- 通常这个样
else{for(int i=var; i<A.size(); i++){temp.push_back(A[i]); backtrack(params); temp.pop_back();}}
- 通常这个样
- 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, 重要)的更多相关文章
- 刷题46. Permutations
一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- [Leetcode][Python]46: Permutations
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 46: Permutationshttps://leetcode.com/pr ...
- 46. Permutations 排列数
46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations. For ex ...
- 47. Permutations II(medium, backtrack, 重要, 条件较难思考)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 46. Permutations (Back-Track,Sort)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode:46. Permutations(Medium)
1. 原题链接 https://leetcode.com/problems/permutations/description/ 2. 题目要求 给定一个整型数组nums,数组中的数字互不相同,返回该数 ...
- 46 Permutations(全排列Medium)
题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
随机推荐
- Angular 学习笔记 ( PWA + App Shell )
PWA (Progressive Web Apps) 是未来网页设计的方向. 渐进式网站. Angular v5 开始支持 pwa 网站 (所谓支持意思是说有一些 build in 的方法和规范去实现 ...
- python 开发之路 - 入门
一. python 介绍 Python是著名的"龟叔"Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言.1991年 发布Python ...
- Commons-FileUpload组件的应用
<% request.setCharacterEncoding("utf-8"); String uploadFileName="";//上传的文 ...
- SpringBoot中@ManyToMany的坑
我在User表中添加了manytomany的外键映射 @ManyToMany(fetch=FetchType.EAGER) @JoinTable(name="user_role", ...
- https原理通俗了解
摘要:本文尝试一步步还原HTTPS的设计过程,以理解为什么HTTPS最终会是这副模样.但是这并不代表HTTPS的真实设计过程.在阅读本文时,你可以尝试放下已有的对HTTPS的理解,这样更利于" ...
- thinkjs升级到3.0后的图片上传
似乎当thinkjs升级到3.0后,才接手了一个项目.只是在实际运用过程中,还是发现了与2.2的些许差别——今天先分享关于图片上传的一些问题. 1.上传文件,我们选择了jQuery的插件:http:/ ...
- ES6 继续 变量的解构赋值
春节放假这几天,感觉跟梦一样,瞬间就过去了.现在上班的前几天,都感觉有点不真实,不过看到口袋里的钱,就知道,是真真实实的度过了这个假期. 现在得开始重新工作了: 变量的解构赋值 ES6 允许按照一定模 ...
- Sevrlet 工作原理解析-转
从 Servlet 容器说起 要介绍 Servlet 必须要先把 Servlet 容器说清楚,Servlet 与 Servlet 容器的关系有点像枪和子弹的关系,枪是为子弹而生,而子弹又让枪有了杀伤力 ...
- input和textarea标签的select()方法----选中文本框中的所有文本
JavaScript select()方法选中文本框中的所有文本 <input>和<textarea>两种文本框都支持select()方法,这个方法用于选择文本框中的所有文本 ...
- 机器学习技法:03 Kernel Support Vector Machine
Roadmap Kernel Trick Polynomial Kernel Gaussian Kernel Comparison of Kernels Summary