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 ...
随机推荐
- 阿里云API网关(15)监控预警
网关指南: https://help.aliyun.com/document_detail/29487.html?spm=5176.doc48835.6.550.23Oqbl 网关控制台: https ...
- Python学习之再议row_input
再议raw_input birth = raw_input('birth: ') if birth < 2000: print '00前' else: print '00后' 运行结果: bir ...
- 记录项目中用的laypage分页代码
最终才觉得,好记性不如烂笔头,毕竟已经不是刚毕业时候的巅峰了,精力有所下降,很多时候记不住东西. 参考url:http://www.layui.com/laypage/ 直接上代码了 <scri ...
- Python django实现简单的邮件系统发送邮件功能
Python django实现简单的邮件系统发送邮件功能 本文实例讲述了Python django实现简单的邮件系统发送邮件功能. django邮件系统 Django发送邮件官方中文文档 总结如下: ...
- python github
git 1. 版本控制 是否依稀记得你的毕业论文? 1 2 3 4 5 6 7 8 9 10 11 毕业论文_初稿.doc 毕业论文_修改1.doc 毕业论文_修改2.doc 毕业论文_修改3.doc ...
- API之实用工具Postman 使用方法
测试接口与文档信息文件 Postman 安装与入门教程 下载与安装 官方网站:www.getpostman.com 下载完成后,直接安装 输入2次邮箱,密码,即可注册并登陆! 开发者使用: 创建文件夹 ...
- POJ-1287 Networking---裸的不能再裸的MST
题目链接: https://vjudge.net/problem/POJ-1287 题目大意: 模板 #include<iostream> #include<cstdio> # ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- 在CentOS 7+ 安装Kubernetes入门
TL;DR; 科学上网,科学上网,科学上网,重要的事情说三次.如果不会科学上网,这篇文章就没有看下去的意义.作为一个技术人员如果不愿意折腾,很难有所作为.作为一个单纯的技术人员,最好把心思放在技术上, ...
- ls-dyna基础教程
刚刚开始使用ls-dyna,几天前还只知道点开dyna界面,然后就没有然后了,没人带,资料也没多少,但是科研还得继续往下做呀(手动滑稽),通过在仿真论坛上搜索相关的资料,并通过自己的一步步操作,做了大 ...