zoukankan      html  css  js  c++  java
  • 一个经典的排列组合面试题目

    有一个关于排列组合的经典面试题目,拿来分析一下。

    题目如下:

    1、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列, 如:512234、412345等.要求:"4"不能在第三位,"3"与"5"不能相连.

    第一种解决思路。 列出所有的组合,然后看看哪个不符合要求,剔除。 对于计算机来说。这种方是可行的,而且思路清晰。

    public class test1 {
        private int[] numbers = new int[] { 1, 2, 3, 3, 4, 5 };
        public int n;
        private String lastResult = "";
    
        private boolean validate(String s) {
            if (s.compareTo(lastResult) <= 0)
                return false;
            if (s.charAt(2) == '4')
                return false;
            if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0)
                return false;
            return true;
        }
    
        public void list(String index, String result) {
            for (int i = 0; i < numbers.length; i++) {
                if (index.indexOf(i + 48) < 0) {
                    String s = result + String.valueOf(numbers[i]);
                    if (s.length() == numbers.length) {
                        if (validate(s)) {
                            System.out.println(s);
                            lastResult = s;
                            n++;
                        }
                        break;
                    }
                    list(index + String.valueOf(i), s);
                }
            }
        }
    
        public static void main(String[] args) {
            test1 t = new test1();
            t.list("", "");
            System.out.println("总数:" + t.n);
        }
    }

    第二种解决思路

       把问题归结为图结构的遍历问题。实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是 最后对这6个数字的排列组合结果集。 2 显然这个结果集还未达到题目的要求。从以下几个方面考虑: 1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。 2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果 3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。 采用二维数组定义图结构

      

    public class TestQuestion { 
     
    private String[] b = new String[]{"1", "2", "2", "3", "4", "5"}; 
    private int n = b.length; 
    private boolean[] visited = new boolean[n]; 
    private int[][] a = new int[n][n]; 
    private String result = ""; 
    private TreeSet set = new TreeSet(); 
     
    public static void main(String[] args) { 
    new TestQuestion().start(); 
    } 
     
    private void start() { 
     
    // Initial the map a[][] 
    for (int i = 0; i < n; i++) { 
    for (int j = 0; j < n; j++) { 
    if (i == j) { 
    a[i][j] = 0; 
    } else { 
        a[i][j] = 1; 
    } 
    } 
    } 
     
    // 3 and 5 can not be the neighbor. 
    a[3][5] = 0; 
    a[5][3] = 0; 
     
    // Begin to depth search. 
    for (int i = 0; i < n; i++) { 
        this.depthFirstSearch(i); 
    } 
     
    // Print result treeset. 
    Iterator it = set.iterator(); 
    while (it.hasNext()) { 
    String string = (String) it.next(); 
    // "4" can not be the third position. 
    if (string.indexOf("4") != 2) { 
    System.out.println(string); 
    } 
    } 
    } 
     
    private void depthFirstSearch(int startIndex) { 
    visited[startIndex] = true; 
    resultresult = result + b[startIndex]; 
    if (result.length() == n) { 
    // Filt the duplicate value. 
    set.add(result); 
    } 
    for(int j = 0; j < n; j++) { 
    if (a[startIndex][j] == 1 && visited[j] == false) { 
    depthFirstSearch(j); 
    } else { 
    continue; 
    } 
    } 
     
    // restore the result value and visited value after listing a node. 
        resultresult = result.substring(0, result.length() -1); 
        visited[startIndex] = false; 
    } 
    }
  • 相关阅读:
    vsCode 使用 PHP Intelephense插件函数跳转跟踪
    acme.sh 生成证书一直卡在Getting domain auth token for each domain
    用命令行执行php脚本输出乱码
    js检测是否是手机平台
    个人知识结构索引
    离线环境下安装ansible,借助有网环境下pip工具
    zabbix之微信告警(python版):微信个人报警,微信企业号告警脚本
    使用ansible结合FTP部署zabbix_agent
    datax的可视化-datax-web
    Finereport决策报表
  • 原文地址:https://www.cnblogs.com/echomyecho/p/3257308.html
Copyright © 2011-2022 走看看