Permutations
来源:互联网 发布:上海华讯网络 编辑:程序博客网 时间:2024/06/04 18:56
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[] a) { List<List<Integer>> result = new ArrayList<List<Integer>>(); List<Integer> list = new ArrayList<Integer>(); helper(a, list, result); return result; } private void helper(int[] nums, List<Integer> list,List<List<Integer>> ret) { if (list.size() == nums.length) { ret.add(new ArrayList<Integer>(list)); return; } for (int i = 0; i < nums.length; i++) { if (list.contains(nums[i])) continue; list.add(nums[i]); helper(nums, list, ret); list.remove(list.size() - 1); } }}
0 0
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- 现代社会之--丛林法则【转载】
- URL中“#” “?” &“”号的作用
- Spring @Transaction配置示例及发生不回滚原因深度剖析
- highcharts统计的应用
- 新方法实现杨辉三角
- Permutations
- Java 类的卸载机制
- 一个微软面试题--关于位结构体
- std::function
- 收集大牛博客
- windows x32 安装mongodb
- 神经网络学习笔记 - lecture3:The backpropagation learning proccedure
- 找点 nyoj 891
- HDU 5296 Annoying Problem 树链剖分 LCA 倍增法