Question

Given a collection of 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], and [3,2,1].

Solution

Traditional backtracking  way to solve this problem.

 public class Solution {
public List<List<Integer>> permute(int[] nums) {
Arrays.sort(nums);
int length = nums.length;
List<List<Integer>> result = new ArrayList<List<Integer>>();
boolean[] visited = new boolean[length]; dfs(nums, visited, new ArrayList<Integer>(), result);
return result;
} private void dfs(int[] nums, boolean[] visited, List<Integer> record, List<List<Integer>> result) {
if (record.size() == nums.length) {
if (!result.contains(record))
result.add(new ArrayList<Integer>(record));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!visited[i]) {
record.add(nums[i]);
visited[i] = true;
dfs(nums, visited, record, result);
// Restore
record.remove(record.size() - 1);
visited[i] = false;
}
}
}
}

Another solution is that we don't need to store visited status, but we just need to modify "nums" object.

 class Solution:
def dfs(self, nums: List[int], record: List[int], result: List[List[int]]) -> None:
if not nums:
result.append(record)
for i in range(len(nums)):
self.dfs(nums[:i] + nums[i + 1:], record + [nums[i]], result) def permute(self, nums: List[int]) -> List[List[int]]:
result = []
self.dfs(nums, [], result)
return result

Permutations 解答的更多相关文章

  1. Palindrome Permutation II 解答

    Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...

  2. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  3. LeetCode OJ 47. Permutations II

    题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...

  4. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  5. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  6. 刷题46. Permutations

    一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...

  7. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  9. [LeetCode] Permutations 全排列

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

随机推荐

  1. javaweb笔记2之HTTP协议

    1 什么是http协议 是浏览器客户端  和  服务器端 数据传输的 格式规范: 2 查看http协议 (1)用火狐的firebug插件查看      (2)使用谷歌的Ghome查看(审查元素-> ...

  2. [Qt] QString 和 char* 转换

    (1) QString 转 char* char acResult[10240]; //QByteArray baResult = strResult.toLatin1(); QByteArray b ...

  3. js-权威指南学习笔记4

    第五章 语句 1.在JS中没有块级作用域,在语句块中声明的变量并不是语句块私有的. 2.尽管函数声明语句和函数定义表达式具有相同的函数名,但二者仍然不同.两种方式都创建了新的函数对象,但函数声明语句中 ...

  4. nginx 配置正向 HTTP 代理服务器[转]

    如果不想写到 ngnix.conf 中,那么可以在相同的目录下建立另外一个文件夹存放单独的文件,比如新建一个  proxy 的子目录,然后再在里面新建文件 prox.conf ,然后添加如下内容: s ...

  5. Appium依据xpath获取控件实例随笔

    如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.当中一种就是依据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...

  6. Openstack 二次开发之:在windows 环境下编译Openstack-java-sdk

    在windows环境下使用maven对openstack-java-sdk进行编译 编译源文件 下载源代码 git clonehttps://github.com/woorea/openstack-j ...

  7. LSI SAS 2308配置操作

    介绍LSISAS2308的配置操作 3.1 登录CU界面 介绍登录LSISAS2308的CU配置界面的方法. 3.2 创建RAID 介绍在LSISAS2308扣卡上创建RAID的操作方法. 3.3 配 ...

  8. linux文件解-压缩

    常用: 解压tar.gz包  使用命令:tar -zxvf  file.tar.gz   -z 指有gzip的属性  -x 解开一个压缩文件的参数  -v解压过程中显示文件  -f放最后接filena ...

  9. DC综合环境的一些概念

    DC综合环境的一些概念 启动文件 .synopsys_dc_setup 采用Tcl格式,包含工艺库的路径信息和其他环境变量 不同位置启动顺序 1.Synopsys安装目录 2.用户家目录 3.项目工作 ...

  10. 国外.net学习资源网站

    转载 :出处:http://www.cnblogs.com/kingjiong/ 名称:快速入门地址 http://chs.gotdotnet.com/quickstart/ 描述:本站点是微软.NE ...