【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
来源:互联网 发布:云计算和超级计算机 编辑:程序博客网 时间:2024/05/01 00:27
【046-Permutations(求排列)】
【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
原题
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]
.
题目大意
给定一个数组,返回他的所有排列。
解题思路
使用分治法求解。
代码实现
算法实现类
import java.util.*;public class Solution { private List<List<Integer>> result; public List<List<Integer>> permute(int[] num) { result = new LinkedList<>(); if (num != null) { permute(0, num); } return result; } private void permute(int i, int[] num) { if (i == num.length) { List<Integer> l = new ArrayList<>(); for (int n: num) { l.add(n); } result.add(l); }else { for (int j = i; j < num.length; j++) { swap(num, j, i); permute(i + 1, num); swap(num, j, i); } } } private void swap(int[] A, int x, int y) { int tmp = A[x]; A[x] = A[y]; A[y] = tmp; }}
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。
特别说明
欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47098351】
2 0
- 【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
- leetcode:Permutations (序列所有排列)【面试算法题】
- 【LeetCode-面试算法经典-Java实现】【001-Two Sum(求两个数的和)】
- leetcode:Permutations II (序列所有无重复排列)【面试算法题】
- 【LeetCode-面试算法经典-Java实现】【078-Subsets(子集)】
- 【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】
- leetcode:Next Permutation (求下一个排列) 【面试算法题】
- 【LeetCode-面试算法经典-Java实现】【050-Implement pow(x, n)(求x的n次方)】
- 笔试面试算法经典--全排列算法-递归&字典序实现(Java)
- 全排列算法递归实现(Permutations)
- LeetCode:Permutations(全排列算法的递归与非递归实现)
- LeetCode | Permutations(全排列)
- LeetCode 46. Permutations(排列)
- 【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
- 【LeetCode-面试算法经典-Java实现】【所有题目目录索引】
- 【LeetCode-面试算法经典-Java实现】【028-Implement strStr() (实现strStr()函数)】
- leetcode 47. Permutations II-排列|回溯算法
- 【LeetCode-面试算法经典-Java实现】【005-Longest Palindromic Substring(最长回文子串)】
- 在eclipse下配置maven
- 本地自旋锁与信号量/多服务台自旋队列-spin wait风格的信号量
- associative
- hdu 1328 IBM Minus One
- 【LeetCode-面试算法经典-Java实现】【038-Count and Say(计数和表述)】
- 【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
- Linux jdk安装与卸载
- 【LeetCode-面试算法经典-Java实现】【050-Implement pow(x, n)(求x的n次方)】
- 初用python的几点总结
- [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第九章 内存管理
- [读书笔记—学习方法]《深度学习的艺术》-采铜
- table包河AbstractTableModel抽象类
- C#设计界面时,未将对象引用设置到对象实例问题解决方案
- IEDiagnosticsAdapter (IE诊断适适配)在IE和Chrome开发者工具之间搭桥