46. Permutations
来源:互联网 发布:百度地图lbs数据 编辑:程序博客网 时间:2024/06/10 05:50
Given a collection of distinct 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],
[3,2,1]
]
问题描述:这是一个全排列问题,解决方法为:设置一个visited数组来记录这个数字是否已经被排列,然后再从开始进行循环。例如:【1,2,3,4】加入是第三次的话,3被排列,visited即被标记为true,下次循环的时候就会跳过3,从而达到了全排列的目的。具体代码如下:
public List<List<Integer>> permute(int[] num) { List<List<Integer>> result=new ArrayList<List<Integer>>(); List<Integer> temp=new ArrayList<Integer>(); if (num==null||num.length==0) return result; boolean[] visited=new boolean[num.length]; permute(num,result,temp,visited); return result; } public static void permute(int[] num,List<List<Integer>> result,List<Integer> temp,boolean[] visited) { if (temp.size()==num.length) { result.add(new ArrayList<>(temp)); return; } for (int i = 0; i < num.length; i++) { if (!visited[i]) { temp.add(num[i]); visited[i]=true; permute(num, result, temp, visited); temp.remove(temp.size()-1); visited[i]=false; } } }
0 0
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- 46. Permutations
- c 语言中的宏定义
- android通过webservice连接SQL数据库(一)服务器端
- tensorflow线性回归例子
- 贪心算法基础之活动时间安排(二) 51nod 贪心教程
- 腾讯加固脱壳
- 46. Permutations
- Binder的使用
- Linux下抓包工具tcpdump的使用
- 快排算法C++实现
- 【linux】编写一个简单的shell
- Cocos2d-x lua学习笔记
- 数据结构与算法(18)——二叉树习题一
- SQLiteDatabase 操作大全
- maven多模块创建-实战(2)