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 ...
随机推荐
- [原创]HTML5 web性能监控策略
web性能重要指标--时长 通常在监控前端页面性能的时候,我们会需要获取到很多的时间戳,比如用户按下回车的时候开始计时,但这个时候,我们统计的js代码并没有加载过来,也无法读取到相关的信息.在HTML ...
- 利用eclipse新建的java web项目没有部署描述符web.xml文件怎么办?
原文转自:http://blog.csdn.net/suyu_yuan/article/details/50947007 利用eclipse新建的Java Web项目没有部署描述符web.xml文件, ...
- 魔方公式xyz
x:(整个魔方以R的方向转动),x':(整个魔方以R'的方向转动) y:(整个魔方以U的方向转动),y':(整个魔方以U'的方向转动) z:(整个魔方以F的方向转动),z':(整个魔方以F'的方向 ...
- Face recognition using Histograms of Oriented Gradients
Face recognition using Histograms of Oriented Gradients 这篇论文的主要内容是将Hog算子应用到人脸识别上. 转载请注明:http://blog. ...
- [React] React Router: Route Parameters
A router library is no good if we have to hardcode every single route in our application. In this le ...
- Java基础知识强化45:StringBuffer类之字符串反转的案例
1. 案例演示: package cn.itcast_07; import java.util.Scanner; /* * 把字符串反转 */ public class StringBufferTes ...
- 广播接收者 BroadcastReceiver 示例-2
BaseActivity /**所有Activity的基类*/ public class BaseActivity extends Activity { @Override prote ...
- 理解JavaScript中作用域链的关系
javascript里的关系又多又乱.作用域链是一种单向的链式关系,还算简单清晰:this机制的调用关系,稍微有些复杂:而关于原型,则是prototype.proto和constructor的三角关系 ...
- hdu 2032
水题 AC代码: #include <stdio.h> int main() { int a[31][31]; int i,j,n; a[1][1]=a[2][1]=a[2][2]=1; ...
- javascript sort 用法
<html> <head> <title></title> <script type="text/javascript" sr ...