zoukankan      html  css  js  c++  java
  • 全排列

    全排列

    一 问题描述
    给出一串字符的全排列

    二 问题分析
    采用回溯算法之排列树
    三 代码实现

    package backtracking_perm;
    
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.LinkedList;
    
    import javax.rmi.PortableRemoteObject;
    
    public class bin
    {
        public static void main(String[] args) throws IOException
        {
            char []x={'A','B','C','D'};
            perm myPerm=new perm(x);
        }
    }
    class perm
    {
        char []ch;
        char []x;
        LinkedList<String> resultSet=new LinkedList<String>();
        public perm(char []x) throws IOException
        {
            this.x=x;
            this.ch=Arrays.copyOf(x, x.length);
            Perm(0);
            display();
        }
        public void Perm(int k)  //k表示处理位置
        {
            //制造排列树
            if(k==x.length-1)   //叶子结点  递归出口
            {
                resultSet.add(String.valueOf(x));  //为何无法直接添加x
    //			System.out.println(String.valueOf(x));
                return;
            }
            for(int i=k; i<x.length; i++)
            {
                char t=x[i];
                x[i]=x[k];
                x[k]=t;
                Perm(k+1);
                t=x[i];
                x[i]=x[k];
                x[k]=t;
            }
        }
        public void display() throws IOException
        {
            BufferedWriter fout=new BufferedWriter(new FileWriter("out.txt"));
            fout.write("ch[i]:");
        	fout.newLine();
        	fout.write(String.valueOf(ch));
        	fout.newLine();
        	fout.flush();
        	fout.write("x[i]:");
        	fout.newLine();
        	for(int i=0; i<resultSet.size(); i++)
        	{
        		fout.write(String.valueOf(resultSet.get(i)));
        		fout.newLine();
        	}
        	fout.newLine();
        	fout.flush();
        }
        
    }
    

    四 运行结果
    enter description here
    五总结收获

    1. 熟练回溯法
    2. 使用Linkedlist类
    3. Sting.valueOf();
      六不足

    1.手速太慢
    2. 做题太少

  • 相关阅读:
    初识 vue
    Spring boot 整合 Swagger
    Swagger 注解
    初识 Swagger
    初识 mycat
    SpringBoot中的国际化
    为什么博客园用户体验这么差?
    Numpy常用方法及应用总汇
    嵌入式开发10种常见数字滤波算法
    .gitignore使用
  • 原文地址:https://www.cnblogs.com/Howbin/p/9926195.html
Copyright © 2011-2022 走看看