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("################");

    }
}
时间: 2024-11-07 17:30:23

minimum-genetic-mutation的相关文章

(Java) LeetCode 433. Minimum Genetic Mutation —— 最小基因变化

A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE m

433. Minimum Genetic Mutation

问题描述: A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". Suppose we need to investigate about a mutation (mutation from "start" to "end"), where

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】未分类(tag里面没有)(共题)

[419]Battleships in a Board (2018年11月25日)(谷歌的题,没分类.) 给了一个二维平面,上面有 X 和 . 两种字符. 一行或者一列连续的 X 代表一个战舰,问图中有多少个战舰.(题目要求one pass, 空间复杂度是常数) 题目说了每两个战舰之间起码有一个 . 作为分隔符,所以不存在正好交叉的情况. 题解:我提交了一个 floodfill 的题解,能过但是显然不满足要求. discuss里面说,我们可以统计战舰的最上方和最左方,从而来统计战舰的个数.(这个

几个mutation概念

Somatic mutations: A change in the genetic structure that is not inherited from a parent, and also not passed to offspring, is called a somatic cell genetic mutation or acquired mutation A germline mutation is any detectable and heritable variation i

苯氧乙醇Phenoxyethanol

Irritation (skin, eyes, or lungs), Occupational hazards, Use restrictions, Organ system toxicity (non-reproductive), Allergies/immunotoxicity 防腐剂.定香剂,未稀释前对眼睛刺激性很大,稀释至2.2%则不具刺激性. 属低度皮肤过敏危险性 美国食物及药物管理局(FDA)指,Phenoxyethanol能够抑制中央神经系统,有可能导致呕吐和腹泻. 资料来源:FD

L198

One of the most common birth defects throughout the world is a cleft lip. Babies born with a cleft lip may also have a cleft裂开的 palate, where the roof of the mouth is split. These birth defects can be repaired surgically. But unless that is done, it

使用遗传算法实现迷宫游戏(genetic maze)

强烈推荐一本书 <游戏编程中的人工智能技术>(AI.Techniques.for.Game.Programming).(美)Mat.Buckland 一.缘起 在之前的c印记系列当中有有一个迷宫小游戏,算是一个关于数组应用的例子. 其中有通过接收按键(人工操作)的方式来走出迷宫,也有使用递归算法或非递归算法的方式来实现自动(AI操作)走出迷宫. 后来我对近两三年比较火的人工智能,机器学习,深度学习之类的比较感兴趣了.于是乎,我找了很多书籍或网上的文章来看.但基本上都是两个类别的,其中一类就是一

2. Genetic Algorithm(1) ——进化算法

本篇博文讲述基因算法(Genetic Algorithm),基因算法是最著名的进化算法. 内容依然来自博主的听课记录和教授的PPT. Outline 简单基因算法 个体表示 变异 重组 1. 简单基因算法(Simple Genetic Algorithm) Holland's早期的基因算法被认为是“简单的基因算法”或是“权威的基因算法”.(simple genetic algorithm or canonical genetic algorithm) 1. 直接举例说明 问题描述:利用遗传算法求