博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA代码—算法基础:全排列问题
阅读量:4042 次
发布时间:2019-05-24

本文共 2810 字,大约阅读时间需要 9 分钟。

全排列问题

问题描述

给定一个整型数组,例如:[1,2,3],输出元素的全排列形式,即:

[[3, 2, 1], [2, 3, 1], [2, 1, 3], [3, 1, 2], [1, 3, 2], [1, 2, 3]]

算法分析

当只有一个元素时,集合为:{1}

现在加入2,将数字2分别加入上述集合的 0位置和1位置,
可以得到:{2,1};{1,2}
当在加入3时,
可以将数字3分别插入集合{2,1},和集合{1,2}的 0,1,2位置
对于集合{2,1},数字3插入的位置为0时,得到{3,2,1};数字3插入的位置为1时,得到{2,3,1};数字3插入的位置为2时,得到{2,1,3}
对于集合{1,2},数字3插入的位置为0时,得到{3,1,2};数字3插入的位置为1时,得到{1,3,2};数字3插入的位置为2时,得到{1,2,3}

两个集合,可以选择3个插入位置:2*3 = 6,全排列为6种。

最终得到:{3,1,2};{3,2,1};{1,3,2};{2,3,1};{1,2,3};{2,1,3}

算法设计

给出第一种算法设计

public List
> permute(int[] num) { List
> ans = new ArrayList
>(); if (num.length ==0) return ans; List
l0 = new ArrayList
(); l0.add(num[0]); ans.add(l0); for (int i = 1; i< num.length; ++i){ List
> new_ans = new ArrayList
>(); for (int j = 0; j<=i; ++j){ for (List
l : ans){ List
new_l = new ArrayList
(l); new_l.add(j,num[i]); new_ans.add(new_l); } } ans = new_ans; } return ans; }

完整的代码如下:

package com.bean.algorithmbasic;import java.util.ArrayList;import java.util.List;public class PermutationsDemo2 {
public List
> permute(int[] num) { List
> ans = new ArrayList
>(); if (num.length ==0) return ans; List
l0 = new ArrayList
(); l0.add(num[0]); ans.add(l0); for (int i = 1; i< num.length; ++i){ List
> new_ans = new ArrayList
>(); for (int j = 0; j<=i; ++j){ for (List
l : ans){ List
new_l = new ArrayList
(l); new_l.add(j,num[i]); new_ans.add(new_l); } } ans = new_ans; } return ans; } public static void main(String[] args) { // TODO Auto-generated method stub PermutationsDemo2 pd=new PermutationsDemo2(); int[] array= { 1,2,3}; List
> list=pd.permute(array); System.out.println(list); }}

给出第二种算法设计

package com.bean.algorithmbasic;import java.util.ArrayList;import java.util.List;public class PermutationsDemo {
public List
> permute(int[] nums) { List
> permutations = new ArrayList<>(); if (nums.length == 0) { return permutations; } collectPermutations(nums, 0, new ArrayList<>(), permutations); return permutations; } private void collectPermutations(int[] nums, int start, List
permutation, List
> permutations) { if (permutation.size() == nums.length) { permutations.add(permutation); return; } for (int i = 0; i <= permutation.size(); i++) { List
newPermutation = new ArrayList<>(permutation); newPermutation.add(i, nums[start]); collectPermutations(nums, start + 1, newPermutation, permutations); } } public static void main(String[] args) { // TODO Auto-generated method stub PermutationsDemo pd=new PermutationsDemo(); int[] array= { 1,2,3}; List
> list=pd.permute(array); System.out.println(list); }}

(完)

转载地址:http://cwtdi.baihongyu.com/

你可能感兴趣的文章
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>
标记一下
查看>>
IP报文格式学习笔记
查看>>