• minimum-genetic-mutation


    题目还是很好的,提供了一种新的思路方向。

    细节方面,开始我的判断条件用的dict,不太好,后来debug好了。

    另外,注意其中用数组初始化Set的方法:Set<String> dict = new HashSet(Arrays.asList(bank));

    还有 Set<String> a = new Hash<>(); 当中前面<>里面加String的原因是提取出来的直接就是String,后面<>指的是接受各种类型。不加<>表示不区分类型,应该和<>是等价的效果。

    https://leetcode.com/problems/minimum-genetic-mutation/
    
    // 这个方法特别好
    // https://discuss.leetcode.com/topic/63959/c-two-end-bfs-solution-exactly-same-as-127-word-ladder
    // 提到与 https://leetcode.com/problems/word-ladder/ 127. Word Ladder 题目其实是一模一样的
    // 看了一下,的确是的
    
    public class Solution {
        // 这个题目里面用到了两面BFS的方法,然后通过对字符串的替换,来降低优先级
        // 之前另一个题目 Word Ladder里面,用的是一端到另一端的
        // 现在这个更巧妙一些,因为从set里面找结果,比遍历set的内容要更高效一些
        public int minMutation(String start, String end, String[] bank) {
            Set<String> dict = new HashSet(Arrays.asList(bank));
            if (!dict.contains(end)) {
                return -1;
            }
    
            Set<String> lessSet = new HashSet();
            Set<String> moreSet = new HashSet();
    
            char[] chList = {'A', 'C', 'G', 'T'};
    
            lessSet.add(start);
            moreSet.add(end);
            int step = 0;
            while (true) {
    
                // wrong: if dict is empty, no need to check more
                // right: check lessSet
                if (lessSet.isEmpty()) {
                    return -1;
                }
    
                if (moreSet.size() < lessSet.size()) {
                    Set<String> tmp = lessSet;
                    lessSet = moreSet;
                    moreSet = tmp;
                }
                //printSet(lessSet, moreSet, dict);
    
                step++;
                Iterator<String> iter = lessSet.iterator();
                Set<String> newSet = new HashSet();
                while(iter.hasNext()) {
                    String str = iter.next();
                    for (int i=0; i<str.length(); i++) {
                        StringBuilder sb =new StringBuilder(str);
                        for (int j=0; j<4; j++) {
                            if (str.charAt(i) == chList[j]) {
                                continue;
                            }
    
                            sb.setCharAt(i, chList[j]);
                            if (moreSet.contains(sb.toString())) {
                                return step;
                            }
    
                            if (dict.contains(sb.toString())) {
                                dict.remove(sb.toString());
                                newSet.add(sb.toString());
    
                            }
    
                        }
                    }
                }
                lessSet = newSet;
            }
        }
    
        private void printSet(Set<String> lessSet, Set<String> moreSet, Set<String>dict) {
            System.out.printf("Here is lessSet:");
            Iterator<String> iter = lessSet.iterator();
            while (iter.hasNext()) {
                System.out.printf("%s,", iter.next());
            }
            System.out.println();
            System.out.printf("Here is moreSet:");
            iter = moreSet.iterator();
            while (iter.hasNext()) {
                System.out.printf("%s,", iter.next());
            }
            System.out.println();
            System.out.printf("Here is dict:");
            iter = dict.iterator();
            while (iter.hasNext()) {
                System.out.printf("%s,", iter.next());
            }
            System.out.println();
            System.out.println("################");
    
        }
    }
  • 相关阅读:
    设计模式相关,单例模式!
    混合App交互相关,jsBridge的原理
    Vue相关,插槽怎么用!
    Mysql相关,查询功能整理
    图文并茂讲解进程与线程
    线程与进程的区别及其通信方式
    算法,十进制数转任意进制数
    JavaScript,leetcode第198题,打家劫舍
    2.4G无线控制器附加AT2401C功放IC增加距离
    超低功耗WiFi :ESP8089
  • 原文地址:https://www.cnblogs.com/charlesblc/p/5987681.html
Copyright © 2020-2023  润新知