【LeetCode-面试算法经典-Java实现】【046-Permutations(求排列)】
2015-07-28 06:01
603 查看
【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; } }
评测结果
点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。特别说明
欢迎转载,转载请注明出处【/article/1343425.html】
相关文章推荐
- 【LeetCode-面试算法经典-Java实现】【038-Count and Say(计数和表述)】
- php面试题1
- 黑马程序员——11,多线程,同步函数,死锁,一些零散的小知识点
- 最牛的程序员编程讨论
- 前端开发面试知识点大纲
- 黑马程序员——自学总结(六)反射
- 乐观的程序员们
- 程序员是如何一步步走向平庸的?
- 为什么程序员也能成为伟大的CEO
- 程序员修炼指南——引导你成为真正的编程高手
- android面试题总结02 在android中本地写入一个xml文件
- [黑马程序员](第50天)一些平时刷的题(3)
- [黑马程序员](第45天)一些平时刷的题(2)
- [黑马程序员](第40天)一些平时刷的题
- [黑马程序员](第17)异常
- [黑马程序员](第37天)今晚敲的一些代码题
- [黑马程序员](第25天)高新技术之正则表达式
- 【剑指Offer面试题】 九度OJ1390:矩形覆盖
- 程序员的能力拓展模型
- [黑马程序员](第1-5天)基础部分