文章目录

  • 1题目理解
  • 2 回溯
  • 3 47. Permutations II

1题目理解

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
输入:整数数组nums,所有元素不相同
输出:数组的所有可能的排列

例如:
Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

2 回溯

所有排列是指nums中的每一个元素,在每个位置都会出现一次。
有一个长度为n的空数组,第0位元素可能是1、2、3任意一个。第1位也一样。但是这里前面的位置选了的元素,在后面位置就不能使用了。所以,我们可以用一个数组记录哪些元素已经被使用过。

class Solution {private List<List<Integer>> answer;private boolean[] visited;private int[] nums;public List<List<Integer>> permute(int[] nums) {answer = new ArrayList<List<Integer>>();this.nums = nums;visited = new boolean[nums.length];dfs(0,new int[nums.length]);return answer;}private void dfs(int index,int[] items){if(index == this.nums.length){List<Integer> list = new ArrayList<Integer>();for(int n : items){list.add(n);}answer.add(list);return;}for(int i =0;i<nums.length;i++){if(!visited[i]){visited[i] = true;items[index] = nums[i];dfs(index+1,items);visited[i]=false;}}}
}

时间复杂度O(n∗n!)O(n*n!)O(n∗n!)。首先dfs调用次数是n!。在最后结果放入结果集有一个拷贝操作n。所以最终是O(n∗n!)O(n*n!)O(n∗n!)。

3 47. Permutations II

与46类似,但是输入nums可能包含重复元素。
例如:
Input: nums = [1,1,2]
Output:
[[1,1,2],
[1,2,1],
[2,1,1]]
分析:重复元素之前有处理经验了。对数组先排序。
对于第0个位置的元素可能是1、2。选择了第一个1,第二个1就可以跳过了。否则就重复了。
代码只要在之前代码上改一下即可。

class Solution {private List<List<Integer>> answer;private boolean[] visited;private int[] nums;public List<List<Integer>> permuteUnique(int[] nums) {answer = new ArrayList<List<Integer>>();Arrays.sort(nums);this.nums = nums;visited = new boolean[nums.length];dfs(0,new int[nums.length]);return answer;}private void dfs(int index,int[] items){if(index == this.nums.length){List<Integer> list = new ArrayList<Integer>();for(int n : items){list.add(n);}answer.add(list);return;}for(int i = 0;i<nums.length;i++){if(!visited[i]){visited[i] = true;items[index] = nums[i];dfs(index+1,items);visited[i]=false;while(i+1<nums.length && nums[i+1]==nums[i]) i++;}}}
}

时间复杂度最坏情况下上O(n∗n!)O(n*n!)O(n∗n!)。首先dfs调用次数是n!。在最后结果放入结果集有一个拷贝操作n。所以最终是O(n∗n!)O(n*n!)O(n∗n!)。

46. Permutations相关推荐

  1. leetCode 46. Permutations 回溯问题 | Medium

    46. Permutations(全排列问题--回溯问题经典) Given a collection of distinct numbers, return all possible permutat ...

  2. 46. Permutations 排列数

    46. Permutations 题目 Given a collection of distinct numbers, return all possible permutations.For exa ...

  3. LeetCode 46. Permutations

    46. Permutations Given a collection of distinct numbers, return all possible permutations. For examp ...

  4. 【数字全排列】LeetCode 46. Permutations

    LeetCode 46. Permutations Solution0: 补充一个偷鸡摸狗的方法.偷懒的做法直接使用std::next_permutation()函数 class Solution { ...

  5. 46. Permutations (Back-Track,Sort)

    Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the follo ...

  6. 46. Permutations 1

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  7. Leetcode每日一题:46.permutations(全排列)

    思路: 也可以用递归来求解 但是对应开销要大 且递归的核心部分也没有变化 //非递归法 #include <iostream> #include <vector> using ...

  8. 继续过中等难度.0309

      .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Mediu ...

  9. leetcode中求subset、全排列等问题的回溯算法总结

    在leetcode上刷题的时候,偶然看到一位仁兄总结的关于寻找数组的子集(78,90).全排列(46,47).在数组中找出等于固定值的元素的集合(39,40).找出字符串回文子串的集合(131),感觉 ...

最新文章

  1. MySQL 解压版创建用户密码
  2. java编写一个通讯录_java写的通讯录(小玩意)
  3. 鸿蒙45000,华为发布鸿蒙系统沟通口径通知 网友表示支持国货
  4. 微信jssdk 图片上传 JAVA_微信jssdk图片上传
  5. Linux对包管理阐述
  6. sharepoint文档库文档版本信息操作
  7. IOC操作Bean管理XML方式(bean 的生命周期)
  8. Smack 4.3.2 发布,XMPP(jabber) 的 Java 客户端类库
  9. 另类windows与ubuntu共享实现
  10. Python 紧急修复远程代码执行漏洞
  11. go mockweb接口_GitHub - duxiaoman/AnyMock: 通用接口Mock平台
  12. iOS开发技巧:使用Objective-C创建UUID
  13. 基于用户 的协同过滤算法
  14. uniapp编译支付宝小程序图片图标显示问题
  15. 如何用html实现图片轮播,怎么单纯的用html+css实现图片轮播?
  16. android studio 文件名颜色 灰色,绿色,红色,蓝色,白色的含义
  17. 敏捷项目管理传统项目管理的区别
  18. dell客服(dell客服维修中心)
  19. PV值、UV值和IP值
  20. vi linux insert切换该行第一个字符,Linux之Vi命令讲解

热门文章

  1. ArcEngine临时数据存储 创建内存工作空间
  2. Android Studio Gradle构建脚本
  3. error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“MTd_StaticDebug”...
  4. [Java] webservice soap,wsdl 例子
  5. 对PostgreSQL中后台进程内存挂载的初步学习
  6. Linq(03)基础之Orderby group-by
  7. 一个封装了的ADO类,功能非常强大,并做了一个DEMO演示如何操作ACCESS数据库
  8. linux清理备份日志,服务器日志清理备份
  9. oracle的工具cmd,数据库命令行工具DBCLI
  10. java8 入门脚本之家_Java 8中的Lambda表达式