LeetCode46_Permutations
来源:互联网 发布:过山车大亨 mac 编辑:程序博客网 时间:2024/06/05 15:54
Permutations
问题描述
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]]
简单介绍
这道题就是求排列组合问题中的排列问题,在前面LeetCode31 Next Permutation 这道题目中是求下一个排列数。可能会联想这两道题有什么关系,但是实际上解法没有关系。在这道题目中采用动态规划的做法就可以解决问题,简单来说把这些数字组成所有的排列。
采用动态规划的话需要维护一个数组dp[i]代表的是数组中的第i个数字有没有被使用。
具体代码如下
代码
public void help(List<Integer> temp,int[] nums,boolean[] dp){ if(temp.size()==nums.length){ List<Integer> a = new ArrayList<>(temp); result.add(a); }else { for(int i =0;i<nums.length;i++){ if(!dp[i]){ temp.add(nums[i]); dp[i]=true; help(temp,nums,dp); temp.remove(temp.size()-1); dp[i]=false; } } }}
LeetCode学习笔记持续更新,GitHub地址
https://github.com/yanqinghe/leetcode
阅读全文
0 0
- LeetCode46_Permutations
- 插入排序和选择排序
- web
- HTML学习笔记6 表单标签
- PHP单一入口概述与优缺点分析
- git小技巧--如何从其他分支merge个别文件或文件夹
- LeetCode46_Permutations
- jstree 请求后台数据前台处理
- Angular在IE浏览器下的配置
- Redis分布式锁----乐观锁的实现,以秒杀系统为例
- bzoj 4720/Luogu 1850(期望dp)(NOIP 2016)
- Eclipse汉化教程(详细图解)
- javascript 如何判断比较当天24小时的时间大小
- epoll高级应用之同时处理 tcp/udp 的服务器
- C#操作mdb数据库文件