首页 > 代码库 > 算法联系:排列组合
算法联系:排列组合
1. Matrix.java
package net.wuhx.main; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Matrix { private String rowText = ""; private int row = 0; private int col = 0; private Point[][] points = null; public Matrix() { } public Matrix(String text) { this.rowText = text; this.col = text.length(); } public Matrix(String text, int row) { this.rowText = text; this.row = row; this.col = text.length(); this.points = new Point[this.row][this.col]; this.init(); } public void init() { if(!"".equals(rowText) && row != 0 && col != 0 && points!= null) { for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { points[i][j] = new Point(j,i,this.rowText.charAt(j)+""); } } } } public Point[] getChilNode(Point p) { if((p.getY()+1) < points.length) { return points[p.getY()+1]; } return null; } /** * * @return */ public List<String> paiLie() { List<String> list = new ArrayList<String>(); if(this.points != null && this.points.length > 0) { for(Point p : this.points[0]) { sort(list, p, ""); } } return list; } public List<String> paiLie(int m) { if(m > this.col) { System.err.println("Invalid parameter m:"+m); } this.row = m; this.points = new Point[this.row][this.col]; this.init(); return paiLie(); } public void sort(List<String> list, Point p, String path) { if(!path.contains(p.getValue())) { path += p.getValue(); Point[] ps = getChilNode(p); if(ps != null) { for(Point pp : ps) { sort(list, pp, path); } }else { list.add(path); System.out.println(path); } } } public Set<Set> zuHe() { Set<Set> set = new HashSet<Set>(); if(this.points != null && this.points.length > 0) { for(Point p : this.points[0]) { sortSet(set, p, new HashSet<String>()); } } return set; } public void sortSet(Set<Set> set, Point p, Set<String> path) { if(!path.contains(p.getValue())) { path.add(p.getValue()); Point[] ps = getChilNode(p); if(ps != null) { for(Point pp : ps) { sortSet(set, pp, path); } }else { set.add(path); System.out.println(path); } } } public Point[][] getPoints() { return points; } public void setPoints(Point[][] points) { this.points = points; } public int getRow() { return row; } public void setRow(int row) { this.row = row; } public int getCol() { return col; } public void setCol(int col) { this.col = col; } }
package net.wuhx.main; public class Point { private String value; private int x; private int y; public Point() { } public Point(int x, int y) { this.setX(x); this.setY(y); } public Point(int x, int y, String value) { this.setX(x); this.setY(y); this.setValue(value); } public String getValue() { return value; } public void setValue(String value) { this.value = http://www.mamicode.com/value;"{"+value+", ["+x+","+y+"]"+ "}"; } }
package net.wuhx.main; import java.util.List; public class Main { public static void main(String[] args) { // Matrix m = new Matrix("ABCDE", 3); // List<String> list = m.paiLie(); //排列 // Matrix mm = new Matrix("123ABC"); // List<String> list = mm.paiLie(20); // System.out.println(list.size()); //组合 Matrix m = new Matrix("ABCD", 3); m.zuHe(); } }
算法联系:排列组合
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。