Permutations 解答
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 解答的更多相关文章
- Palindrome Permutation II 解答
Question Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
- Cracking the coding interview--问题与解答
http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...
- LeetCode OJ 47. Permutations II
题目 Given a collection of numbers that might contain duplicates, return all possible unique permutati ...
- LeetCode题目解答
LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...
- LeetCode算法题目解答汇总(转自四火的唠叨)
LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...
- 刷题46. Permutations
一.题目说明 题目是46. Permutations,给一组各不相同的数,求其所有的排列组合.难度是Medium 二.我的解答 这个题目,前面遇到过类似的.回溯法(树的深度优先算法),或者根据如下求解 ...
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
随机推荐
- ASP.NET MVC4.0 部署
EntifyFramework 5.0.0 安装 http://www.nuget.org/packages/EntityFramework/5.0.0 1. 文章,部署前的配置 http://www ...
- [置顶] .net技术类面试、笔试题汇总3
今天本人从成都回到了学校,深刻认识了自己存在很多不足,在这段期间会更加努力,争取早日找到一个好工作! 41.在ASP.NET中有Button控件myButton,要是单击控件时,导航到其他页面http ...
- 高速排序-c++(分别用数组和容器实现)
/********************************************************************** *版权全部 (C)2014, cheng yang. * ...
- sqlite3经常使用命令&语法
http://blog.csdn.net/linchunhua/article/details/7184439 sqlite数据库仅仅用一个文件就ok,小巧方便,所以是一个很不错的嵌入式数据库,SQL ...
- Linux安装mysql源码
1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件 (1)先安装cmake(mysql5.5以后是通过cmake来编译的) [root@ rhel5 ...
- coco2d-html5制作弹弓射鸟第一部分---橡皮筋
一.写在前面的话 最近在学习cocos2d-html5方面的知识,一直想从事游戏方面的工作,自己也找了好多资料.网上关于cocos2d-html5的教程真的不多,也只有自己摸索,慢慢看示例代码.由于本 ...
- 使用spring @Scheduled注解运行定时任务、
曾经框架使用quartz框架运行定时调度问题. 老大说这配置太麻烦.每一个调度都须要多加在spring的配置中. 能不能降低配置的量从而提高开发效率. 近期看了看spring的 scheduled的使 ...
- Win32函数Sleep的精度测试
用了三种方法,第一种使用高精度性能计数器:第二种是使用多媒体定时器,另一种是<Windows图形编程>里提供的CPU周期来获取.推荐第一种方式测量: 先看第一种: #include < ...
- Android 自定义控件玩转字体变色 打造炫酷ViewPager指示器
1.概述 本篇博客的产生呢,是因为,群里的哥们暖暖给我发了个效果图,然后问我该如何实现顶部ViewPager指示器的字体变色,该效果图是这样的: 大概是今天头条的app,神奇的地方就在于,切换View ...
- JavaScripts学习日记——DOM SAX JAXP DEMO4J XPath
今日关键词: XML解析器 DOM SAX JAXP DEMO4J XPath XML解析器 1.解析器概述 什么是解析器 XML是保存数据的文件,XML中保存的数据也需要被程序读取然后使用.那么程序 ...