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. (转)苹果消息推送服务器 php 证书生成

    1.准备好 aps_developer_identity.cer , push.p12这两个证书文件 2. 生成证书如下: openssl x509 -in aps_developer_identit ...

  2. jquery easyui Tab 引入页面的问题

    jQuery Easyui 的tabs插件有两种方式加载某个tab(标签页)上的内容:“href远程请求”和“content本地内容”,本文就两种方式的优缺点进行简单分析和思考. 两者特点: href ...

  3. wxWidgets刚開始学习的人导引(3)——wxWidgets应用程序初体验

    wxWidgets刚開始学习的人导引全文件夹   PDF版及附件下载 1 前言2 下载.安装wxWidgets3 wxWidgets应用程序初体验4 wxWidgets学习资料及利用方法指导5 用wx ...

  4. UVA10765-Doves and bombs(BCC)

    option=com_onlinejudge&Itemid=8&page=show_problem&problem=1706">题目链接 题意:给定一个n个点的 ...

  5. IOS成长之路-Nsstring中搜索方法rangeOfString

    NSString *str1 = @"can you \n speak English"; NSString *str = @"\n"; //在str1这个字符 ...

  6. java 多态,和方法覆盖分析(转)

    多态 (Polymorphism) 大家应该都不陌生,它是我们开发面向对象系统的“老朋友”了 .但是老朋友也会有“烦心”的时候啊,呵呵.有时候 不注意,还真会被它难到.譬如下面这个例子(thank H ...

  7. CAsyncSocket

    本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 它是一个异步非阻塞Socket封装类,CAsyncSocket::Create()有一个参数指明了你想要处理哪些Socket事 ...

  8. sys--system-sysdba-sysoper用户区别

    当Oracle 数据库安装完毕后,系统会自动创建sys和system这两个帐户.1.sys :缺省密码为CHANGE_ON_INSTALL ,且被授予DBA角色system :缺省密码为MANAGER ...

  9. vim的基本使用方法

    头记:vim作为被大多数程序员所推崇的编辑器,是源于它的自由灵活以及令人舒服的输入模式,但对于新手来说无疑是个噩梦(需要记太多的命令), 而作为使用了vim有一段时间的我来说,总结下常用的命令,以备新 ...

  10. Java初转型-MavenWEB项目搭建

    http://www.cnblogs.com/xdp-gacl/p/4054814.html