刷题46. Permutations
一、题目说明
题目是46. Permutations,给一组各不相同的数,求其所有的排列组合。难度是Medium
二、我的解答
这个题目,前面遇到过类似的。回溯法(树的深度优先算法),或者根据如下求解:
我考虑可以用dp做,写了一个上午,理论我就不说了,自己看代码:
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
class Solution{
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> res;
vector<vector<int>> next;
unordered_map<int,vector<vector<int>>> dp;
vector<int> cur;
if(nums.empty()) return res;
cur.push_back(nums[0]);
res.push_back(cur);
dp[1] = res;
int currLength = 2;
for(int j=1;j<nums.size();j++){
res = dp[j];
next.clear();
for(int k=0;k<currLength;k++){
cur.clear();
cur.resize(j+1);
for(int m=0;m<res.size();m++){
cur[k] = nums[j];
int t1=0,t2=0;
while(t2<res[m].size()){
if(cur[t1]!=nums[j]){
cur[t1] = res[m][t2];
}else{
++t1;
cur[t1] = res[m][t2];
}
t1++;
t2++;
}
next.push_back(cur);
cur.clear();
cur.resize(j+1);
}
}
currLength++;
dp[j+1] = next;
}
return dp[nums.size()];
}
};
int main(){
Solution s;
vector<int> nums = {1,2,3,4};
vector<vector<int>> r = s.permute(nums);
for(int i=0;i<r.size();i++){
for(int j=0;j<r[i].size();j++){
cout<<r[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}
性能如下:
Runtime: 8 ms, faster than 98.85% of C++ online submissions for Permutations.
Memory Usage: 9.5 MB, less than 46.27% of C++ online submissions for Permutations.
三、优化措施
dp算法,是按照空间换时间的,所以时间还可以,空间就差了点。
下面是回溯算法的代码,可读性好多了:
class Solution{
private:
vector<vector<int>> result;
vector<int> path;
vector<bool> used;
public:
//枚举每个位置放哪个数
void dfs(const vector<int>&nums,int pos){
if(pos == nums.size()){
result.push_back(path);
return;
}
for(int i=0;i<nums.size();i++){
if(!used[i]){
path.push_back(nums[i]);
used[i] = true;
dfs(nums,pos+1);
used[i] = false;
path.pop_back();
}
}
}
vector<vector<int>> permute(vector<int>& nums) {
if(nums.empty()){
return result;
}
used.resize(nums.size());
dfs(nums,0);
return result;
}
};
刷题46. Permutations的更多相关文章
- [刷题] 46 Permutations
要求 整型数组,每个元素不相同,返回元素所有排列的可能 示例 [1,2,3] [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] 思路 树 ...
- leetcode刷题-46全排列
题目 给定一个 没有重复 数字的序列,返回其所有可能的全排列. 思路 回溯算法 不断取出字符,对剩余字符进行选择 实现 class Solution: def permute(self, nums: ...
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- Leetcode OJ 刷题
Valid Palindrome吐槽一下Leetcode上各种不定义标准的输入输出(只是面试时起码能够问一下输入输出格式...),此篇文章不是详细的题解,是自己刷LeetCode的一个笔记吧,尽管没有 ...
- leetcode刷题指南
转载自:http://blog.csdn.net/lnho2015/article/details/50962989 以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目 ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
- 1、学习算法和刷题的框架思维——Go版
前情提示:Go语言学习者.本文参考https://labuladong.gitee.io/algo,代码自己参考抒写,若有不妥之处,感谢指正 关于golang算法文章,为了便于下载和整理,都已开源放在 ...
- leetcode刷题总结一
大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维 Single Number: 有N个数,其中只有一个数出现了一次,其他都是两次,找出那 ...
- 【刷题记录】BZOJ-USACO
接下来要滚去bzoj刷usaco的题目辣=v=在博客记录一下刷题情况,以及存一存代码咯.加油! 1.[bzoj1597][Usaco2008 Mar]土地购买 #include<cstdio&g ...
随机推荐
- python 聚类分析 k均值算法
dataSet = [ #数据集 # 1 [0.697, 0.460], # 2 [0.774, 0.376], # 3 [0.634, 0.264], # 4 [0.608, 0.318], # 5 ...
- 139. Word Break 以及 140.Word Break II
139. Word Break Given a non-empty string s and a dictionary wordDict containing a list of non-empty ...
- centos7下安装ansible
由于centos7预装了python,因此我们可以跳过python的安装环节(记得关闭防火墙) [root@model ~]# [root@model ~]# python --version Pyt ...
- maven项目打包和运行
1. 添加pom <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</ar ...
- ACM-寻宝
题目描述:寻宝 有这么一块神奇的矩形土地,为什么神奇呢?因为上面藏有很多的宝藏.该土地由N*M个小正方形土地格子组成,每个小正方形土地格子上,如果标有“E”,则表示该格可以通过:如果标有“X”,则表示 ...
- MySQL每日执行
drop event if exists upload_deadline; DELIMITER $$ create event upload_deadline day starts timestamp ...
- 箭头函数arrow funtion
1.定义一个匿名函数常规语法: function (x) { return x * x; } 2.该函数使用箭头函数可以使用仅仅一行代码搞定! x => x * x 箭头函数相当于匿名函数,并且 ...
- POJ 1584:A Round Peg in a Ground Hole
A Round Peg in a Ground Hole Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5741 Acc ...
- EUI库 - 容器
eui.UILayer UILayer是Group的子类它只有一个功能,到放到场景上后,宽高永远和场景宽度一致 Group Group 是自动布局的容器基类.如果包含的子项内容太大需要滚动显示 ...
- 【LeetCode】解数独
做题之前先复习下[STL中的Tuple容器] 我们知道,在Python中,大家都知道tuple这个概念,是一个只读的元素容器,容器内的元素数据类型可以不同,而在CPP中大部分的容器只能储存相同数据类型 ...