leetcode Permutation
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].
求全排列,相当于回溯法中的排列树。
public class Solution {
public List<List<Integer>> permute(int[] nums) {
HashSet<List<Integer>> res = new HashSet<List<Integer>>();
backtrace(0,nums.length,nums,res);
return new ArrayList(res);
}
public void backtrace(int t,int n,int[] nums,HashSet<List<Integer>> res){
if(t>=n){
List<Integer> list = new ArrayList<Integer>();
for(int i = 0;i<n;i++)
list.add(nums[i]);
res.add(list);
}
else{
for(int i = t ;i<n; i++){
swap(nums,i,t);
backtrace(t+1, n, nums, res);
swap(nums,t,i);
}
}
}
public int[] swap(int[] ints, int x, int y) {
int temp = ints[x];
ints[x] = ints[y];
ints[y] = temp;
return ints;
}
}
leetcode Permutation的更多相关文章
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
- [leetcode]Permutation Sequence @ Python
原题地址:https://oj.leetcode.com/submissions/detail/5341904/ 题意: The set [1,2,3,…,n] contains a total of ...
- LeetCode: Permutation Sequence 解题报告
Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...
- leetcode -- permutation 总结
leetcode上关于permutation有如下几题 Permutation Sequence Next Permutation Permutations Permutations II
- LeetCode Permutation in String
原题链接在这里:https://leetcode.com/problems/permutation-in-string/description/ 题目: Given two strings s1 an ...
- LeetCode——Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [Leetcode] Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 网易云课堂_C++开发入门到精通_章节8:设计模式
课时44设计模式简介 设计模式简介 面向对象设计的第一个原则:针对接口编程,而不是针对实现编程 接口->指针 实现->实例 若已存在一个类Class A,现在希望复用Class A,则有以 ...
- sh_脚本语法
介绍: 1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在这个例子中我们使用/bin/sh来执行程序. 当编写脚 ...
- hpux操作系统的关机与重新启动命令
关机 shutdown -hy 0 重新启动: shutdown -ry 0
- Struts2使用Interceptor实现权限控制的应用实例详解
Struts2使用Interceptor实现权限控制的应用实例详解 拦截器:是Struts2框架的核心,重点之重.因此,对于我们要向彻底学好Struts2.0.读源码和使用拦截器是必不可少的.少说了. ...
- 教训:TOJ[4081] God Le wants to know the directory
以前的字符串题本来就弱..2年不写就更弱了.嗯.留作教训 God Le is the most talented ACMer in the TJU-ACM team. When he wants to ...
- NET基础课--配置文件2
1. 使用<appSettings> 简单的配置信息,可以直接放入<appSettings>标记中.如: <?xml version="1.0& ...
- 背包问题递归java
public boolean PackageProblem(int[] arr,int start,int targetLeft,int target) { if(arr.length==0) { S ...
- SQL Performance Improvement Techniques(转)
原文地址:http://www.codeproject.com/Tips/1023621/SQL-Performance-Improvement-Techniques This article pro ...
- RTMP流媒体播放过程(转)
http://blog.csdn.net/leixiaohua1020/article/details/11704355 本文描述了从打开一个RTMP流媒体到视音频数据开始播放的全过程. 注意:RTM ...
- shell全备份脚本(借鉴别人的,在其基础上修复完善了bug)
#!/bin/bash # Shell script to backup MySql database # Last updated: Aug - MyUSER="root" # ...