zoukankan      html  css  js  c++  java
  • (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 mutation is defined as ONE single character changed in the gene string.

    For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.

    Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.

    Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1.

    Note:

    1. Starting point is assumed to be valid, so it might not be included in the bank.
    2. If multiple mutations are needed, all mutations during in the sequence must be valid.
    3. You may assume start and end string is not the same.

    Example 1:

    start: "AACCGGTT"
    end:   "AACCGGTA"
    bank: ["AACCGGTA"]
    
    return: 1

    Example 2:

    start: "AACCGGTT"
    end:   "AAACGGTA"
    bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]
    
    return: 2

    Example 3:

    start: "AAAAACCC"
    end:   "AACCCCCC"
    bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]
    
    return: 3
    

    本题和LeetCode 127. Word Ladder —— 单词接龙完全一样,没有任何区别,利用广度优先搜索查找最短路径。欣慰自己写代码一遍过,直接AC。


    Java

    class Solution {
        public int minMutation(String start, String end, String[] bank) {
            if (bank == null || bank.length == 0) return -1;
            char[] gen = {'A','C','G','T'};
            HashSet<String> bankSet = new HashSet<>();
            for (String s : bank)
                bankSet.add(s);
            Queue<String> q = new LinkedList<>();
            HashMap<String, Integer> res = new HashMap<>();
            res.put(start, 0);
            q.add(start);
            while (!q.isEmpty()) {
                String s = q.poll();
                bankSet.remove(s);
                for (int i = 0; i < s.length(); i++) {
                    char[] next = s.toCharArray();
                    for (char c : gen) {
                        next[i] = c;
                        String nextS = new String(next);
                        if (bankSet.contains(nextS)) {
                            res.put(nextS, res.get(s) + 1);
                            if (nextS.equals(end)) return res.get(nextS);
                            q.add(nextS);
                        }
                    }
                }
            }
            return -1;
        }
    }
  • 相关阅读:
    在mac系统上安装Eclipse,编写java程序
    显示和隐藏Mac隐藏文件的终端命令
    iOS中UIKit——UIDataDetectors(数据检测器)它将电话、邮件、网址等变为链接
    C语言中的关键字
    IOS基础——静态方法(类方法)和实例方法
    IOS基础——alloc、init和new方法
    IOS基础——实例变量四种范围类型
    iOS中UIKit——UIFont得到iOS设备上的系统字体
    iOS中UIKit——UIButton设置边框
    IOS中获取屏幕尺寸
  • 原文地址:https://www.cnblogs.com/tengdai/p/9303045.html
Copyright © 2011-2022 走看看