[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",其长度为 ...
随机推荐
- 【SpringBoot】SpringBoot拦截器实战和 Servlet3.0自定义Filter、Listener
=================6.SpringBoot拦截器实战和 Servlet3.0自定义Filter.Listener ============ 1.深入SpringBoot2.x过滤器Fi ...
- DevExpress中barManager下的toolbar如何在panel中显示
如题,我的Dev Toolbar需要在一个pannel中显示,并且居于最顶部.可是好像默认情况下toolbar都是在窗体的最顶部的,如何设置才能使其位于一个panel的最顶部呢? 解决方案:经过测试, ...
- Day 08 文件操作模式,文件复制,游标
with open:将文件的释放交给with管理 with open('文件', '模式', encoding='utf-8') as f: # 操作 pass a模式:追加写入 # t ...
- eclipse开启时报错问题
eclipse启动时报如下错误: Unable to read workbench state.Workbench UI layout will be reset 不能找到正式的工作台,工作台UI的布 ...
- 解决scipy无法正确安装到virtualenv中的问题
一 . pip的基本操作 安装包: pip/pip3 install ***pkg 卸载包: pip/pip3 uninstall ***pkg 查看已经安装的某个包的信息: pip/pip3 sho ...
- plot
scatter import pandas as pd df_train=pd.read_excel(r"C:\Users\Liugengxin\Desktop\回归.xlsx") ...
- android 获取对权限的选择
一般是第三方软件拦截,再次提示给用户,确认权限的,如360等.(PS 没有设置权限的app 是会崩溃的 ,而是不是弹出权限确认,因为你都没设置这个权限)看了网上很多,确切说没有一个适合我的. 其实用 ...
- Webservices部署在IIS6.0上的一个小问题
部署方式还是跟网站的部署方式一样,可是通过localhost访问一直提示400(bad request)错误. 可以在iis上预览到.在vs上引用的时候怎么都预览不到. 换个思路,把localhost ...
- The 'INFORMATION_SCHEMA.GLOBAL_STATUS' feature is disabled; see the documentation for 'show_compatibility_56'
--从mysql5.7.6开始information_schema.global_status已经开始被舍弃,为了兼容性,此时需要打开 show_compatibility_56 mysql> ...
- Oracle 语句整理
1 查出列当中重复的值 select ip2,count(*) from vm_info group by ip2 having count(*)>1 期中ip2为列名 vm_inf ...