[leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations.
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
题意:
打印全排列
Solution1: Backtracking
形式化的表示递归过程:permutations(nums[0...n-1]) = {取出一个数字} + permutations(nums[0...n-1] - 该数字)
code
/*
Time: O(n!)
Space: O(n!). We allocate space to keep N! paths.
*/ class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
// corner case
if (nums == null || nums.length == 0) return result;
List<Integer> path = new ArrayList<>();
helper(nums, path, result);
return result;
} private void helper(int[] nums, List<Integer> path, List<List<Integer>> result){
// base case
if(path.size() == nums.length){
result.add(new ArrayList<>(path));
return;
} for(int i = 0; i< nums.length; i++){
if(path.contains(nums[i])) continue;
path.add(nums[i]);
helper(nums, path, result);
path.remove(path.size() - 1);
}
}
}
[leetcode]46. Permutations全排列(给定序列无重复元素)的更多相关文章
- [leetcode]47. Permutations全排列(给定序列有重复元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- hunnu 11313 无重复元素序列的最长公共子序列转化成最长递增子序列 求法及证明
题目:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11313 湖师大的比赛,见我的另一篇水题题解,这里要说的 ...
- LeetCode:删除排序链表中的重复元素【83】
LeetCode:删除排序链表中的重复元素[83] 题目描述 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2 输出: 1->2 示 ...
- 力扣(LeetCode)删除排序链表中的重复元素II 个人题解
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字. 思路和上一题类似(参考 力扣(LeetCode)删除排序链表中的重复元素 个人题解)) 只不过这里需要用到一个前 ...
- 【LeetCode】Permutations(全排列)
这道题是LeetCode里的第46道题. 题目要求: 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3 ...
- LeetCode - 46. Permutations
46. Permutations Problem's Link -------------------------------------------------------------------- ...
- leetcode刷题第三天<无重复字符的最长子串>
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 : 输入: "abcabcbb" 输出: 解释: 因为无重复字符的最长子串是 . 示例 : 输入: &quo ...
- leetcode 刷题(3)--- 无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
随机推荐
- 登录ssh提示:ssh_exchange_identification: read: Connection reset by peer error
vim /etc/hosts.allow 添加 sshd : ALL
- PHP如何判断一个数组是一维还是多维
什么叫多维数组呢?多维数组,本质上是以数组作为数组元素的数组. 二维数组又称为矩阵,一个数组的元素如果是一维数组,那么我们就称这个数组是二维数组. 怎么判断一个数组是否是一维数组呢?通过count() ...
- centos6.5虚拟机无法访问外网解决办法
安装了centos6.5虚拟机,使用的是桥接方式.把所有的配置已经写到/etc/sysconfig/network-scripts/ifcfg-eth0中后,发现内网可以ping通,外网却无法访问. ...
- Vsftp安装及配置主动模式/被动模式
第一章.前言 FTP的主动模式(active mode)和被动模式(passive mode) 大多数的TCP服务是使用单个的连接,一般是客户向服务器的一个周知端口发起连接,然后使用这个连接进行通讯 ...
- nginx的proxy_pass路径转发规则最后带/问题
一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...
- python-web自动化-三种等待方式
当有元素定位不到时,比如下拉框,弹出框等各种定位不到时:一般是两种问题:1 .有frame :2.没有加等待 下面学习三种等待方式: 1.强制等待 sleep(xx)这种方法简单粗暴,不管浏览器是否加 ...
- Android 开发 知晓各种id信息 获取线程ID、activityID、内核ID
/** * Returns the identifier of this process's user. * 返回此进程的用户的标识符. */ Log.e(TAG, "Process.myU ...
- Android开发 assets目录
Android资源文件分类: Android资源文件大致可以分为两种: 第一种是res目录下存放的可编译的资源文件: 这种资源文件系统会在R.Java里面自动生成该资源文件的ID,所以访问这种资源文件 ...
- Linux 实时查看tomcat 日志--less命令
查看tomcat日志通常用 tail -n 或者 tail -f 或者grep 或者 vi cat等命令去查看异常信息,但是日志是在不停地刷屏,tail是动态的在变的,我们往往期望从日志最后一行往前 ...
- py库: matplotlib
Matplotlib是一个Python 2D绘图库,可以跨平台生成各种硬拷贝格式和交互式环境的出版品质量图. http://matplotlib.org/ matplotlib官网 http://py ...