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 ...
随机推荐
- spring-oauth-server实践:使用授权方式四:client_credentials 模式的客户端和服务端交互
spring-oauth-server入门(1-11)使用授权方式四:client_credentials 模式的客戶端 一.客户端逻辑 1.界面入口(credentials_access_token ...
- 修改hosts 流畅使用coursera
以管理员权限打开 C盘 -> Windows-> System32 -> drives -> etc -> hosts文件 在hosts文件最后写入 52.84.246 ...
- JavaScript的sleep实现--Javascript异步编程学习
一.原始需求 最近在做百度前端技术学院的练习题,有一个练习是要求遍历一个二叉树,并且做遍历可视化即正在遍历的节点最好颜色不同 二叉树大概长这个样子: 以前序遍历为例啊, 每次访问二叉树的节点加个sle ...
- Struts(二十七):使用token或tokenSession防止表单重复提交
什么是表单重复提交 表单重复提交包括以下几种情况: 前提:不刷新表单页面 1.多次点击“提交”按钮后,重复提交了多次: 2.已经提交成功之后,按“回退”按钮之后,在点击“提交”按钮后,提交成功: 3. ...
- Beautiful Soup常见的解析器
Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,如果我们不安装它,则 Python 会使用 Python默认的解析器,lxml 解析器更加强大,速度更快 ...
- php Redis函数使用总结(string,hash,list, set , sort set )
对于:string, set , sort set , hash 的增,改操作,是同一个命令,但是把它当改操作时,及时成功返回值依旧为0 对于:list结构来说,增删改查自有一套方法. <? ...
- OpenShift实战(五):OpenShift容器监控Metrics
1.创建持久化metric pv卷 [root@master1 pv]# cat metrics.json apiVersion: v1 kind: PersistentVolume metadata ...
- JavaScript数据结构与算法(八) 集合(ECMAScript 6中定义的类似的Set类)
TypeScript方式实现源码 // 特性: // 1. 集合是由一组无序且唯一(即不能重复)的项组成的.这个数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中. // 2. 也 ...
- k8s踩坑记 - kubeadm join 之 token 失效
抛砖引玉 环境 centos 7 amd64 两台 kubernetes 1.10 伴随着k8s1.10版本的发布,前天先在一台机器上搭建了k8s单机版集群,即既是master,也是node,按照经验 ...
- 生物分子gene
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAswAAAHoCAYAAABdBLmnAAAgAElEQVR4nOzdB5gsRfU+/kZyTiIgCJ