随机数生成(永不重复)

public class RandomTreeUtils {

public static void main(String[] args) {
//
// /**
// * 随机种子1
// */
// char[] chars = new char[]{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’,
// ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘0’, ‘1’,
// ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’,
// ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’,
// ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’};
//
// char[] chars = new char[]{‘0’, ‘1’,‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’};
//
char[] chars = new char[]{‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’};

  int treeDeeps = chars.length;
    int key = 7;
    // 最多生成7位数
  if (treeDeeps > key) {
      treeDeeps = key;
    }
  // 返回树形
    ArrayList<Node>[]  treeNodeList = treeNodeByChars(chars, treeDeeps);
   // 循环输出 判断重复
    loopTreeNode(treeNodeList, treeDeeps);
}

private static void loopTreeNode(ArrayList<Node>[] treeNodeList, int treeDeeps){
    //树的子节点List
    ArrayList<Node> list = treeNodeList[treeDeeps];
    int count = 1;
    Map<String, String> map = new HashMap<>();
    for(Node n : list){
        String v = n.getNodeRouteString();
        //出现重复校验
        if(map.containsKey(v)){
            System.out.println(count + "偶也,发现重复了 :" +n.getNodeRouteString());
        }
        count ++;
    }
    System.out.println("end .... " + count);
}

private static ArrayList<Node>[] treeNodeByChars(char[] chars, int treeDeeps){
    //取值:位数 + 1
    ArrayList<Node>[] treeNodeList = new ArrayList[treeDeeps + 1];
    for(int i = 0 ; i < treeNodeList.length ;i++){
        treeNodeList[i] = new ArrayList<>(64);
    }
    //创建跟节点.可以随便给个标识
    Node root = new Node('#');
    // 把跟节点加入树形集合的第一层
    treeNodeList[0].add(root);
    //从root(0) 到 第七层的 父节点需要处理,位数
    for(int deep = 0 ; deep < treeDeeps; deep ++){
        ArrayList<Node> currentNodeList = treeNodeList[deep];
        ArrayList<Node> nextNodeList = treeNodeList[deep+1];
        //遍历每一层的Node
        for(int j = 0 ; currentNodeList != null && j < currentNodeList.size() ; j++){
            Node currentNode = currentNodeList.get(j);
            for(char v : chars){
                //1. 如果路径已经使用过则不再使用
                // TODO 需要处理:abcdefg 情况,连续字符的场景
                if(currentNode.exist(v)){
                    continue;
                }
                //2.创建子节点
                Node child = new Node(v,currentNode.getNodeRouteString(),cloneBooleanArr(currentNode.getRout()));
                //3. 添加子节点
                currentNode.getChildens().add(child);
                //4. 添加自己节点路径
                child.setRout(v);
                //5. 将节点添加到下一辈List中
                nextNodeList.add(child);
            }
        }
    }
    return treeNodeList;
}

public static boolean[] cloneBooleanArr(boolean[]  re){
    if(re == null || re.length == 0){
        return new boolean[0];
    }
    boolean[] aa = new boolean[re.length];
    for(int i = 0 ; i < re.length ; i++){
        aa[i] = re[i];
    }
    return aa;
}


public static class Node{
    public Node() {}
    public Node(char value){
        this.value = value;
    }
    public Node(char value,String pRoute,boolean[] rout) {
        this.value = value;
        this.rout = rout;
        this.routeStr.append(pRoute);
    }

    /**
     * 节点本身的值
     */
    char value;
    /**
     * 子节点,最多有62个(就是种子的最大数目)
     */
    List<Node> childens = new ArrayList<>(62);
    /**
     * 路径节点 路径有没有
     */
    boolean[] rout = new boolean[128];
    /**
     * 路径顺序
     */
    private StringBuilder routeStr = new StringBuilder();

    public char getValue() {
        return value;
    }

    public void setValue(char value) {
        this.value = value;
    }

    public List<Node> getChildens() {
        return childens;
    }
    /**
     * 判断一个值是否存在,最小0是48, z:122, Z:90
     * @param i
     * @return
     */
    public boolean exist(int i){
        return rout[i];
    }
    /**
     * 标识路径存在
     * @param i
     */
    public void setRout(int i){
        rout[i] = true;
        //添加路径
        routeStr.append((char)i);
    }

    public boolean[] getRout() {
        return rout;
    }

    public String getNodeRouteString() {
        return this.routeStr.toString();
    }
}

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值