Permutations
来源:互联网 发布:sql修改字段数据类型 编辑:程序博客网 时间:2024/05/18 03:15
问题描述
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].
思考:递归求解
想法
- 1、设number为存放未被加入序列的List,curNum为已加入序列的list
- 2、逐个将number中的元素加入cueNum,递归,如果number长度为0,则
将curNum中的内容加入result,
代码
public class Solution { public List<List<Integer>> permute(int[] num) { List<List<Integer>> result = new LinkedList<List<Integer>>(); if(num.length == 0) return result; List<Integer> number = new LinkedList<>(); List<Integer> curNum = new LinkedList<>(); for(int i = 0; i < num.length; i++) number.add(num[i]); recurPermutation(number, curNum, result); return result; } private void recurPermutation(List<Integer> number, List<Integer> curNum, List<List<Integer>> result){ if(number.size() == 0){ result.add(new LinkedList<Integer>(curNum)); return; } for(int i = 0; i < number.size(); i++){ int temp = number.remove(i); curNum.add(temp); recurPermutation(number,curNum,result); curNum.remove(curNum.size() - 1); number.add(i,temp); } }}
0 0
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- Permutations
- 租车系统Java版小程序
- 模式之--生成器(Builder)未完
- Spring 与 Spring mvc
- hdoj 1081 To The Max
- React从零开始
- Permutations
- response.setContentType与 request.setCharacterEncoding 区别
- 深入理解iOS API系列(二)UIImage的resizableImageWithCapInsets:resizingMode:
- 接口回调
- 第五周项目3——人数不定的工资类
- vs 开发工具 中开发mfc项目在输出对话框中输出打印的方法
- taglib 标签库分析
- ECSHOP常用修改
- poj1088 滑雪 记忆化搜索