Leetcode: 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: Starting point is assumed to be valid, so it might not be included in the bank.
If multiple mutations are needed, all mutations during in the sequence must be valid.
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
the same with word ladder
写的时候语法上出了一些问题
第5行不用给char数组赋大小
第20行用stringbuilder的时候曾经写成:String afterMutation = new StringBuilder(cur).setCharAt(i, c).toString(); 这会有错因为.setCharAt()函数返回值是void;替代方法可以是char[] array = string.toCharArray(); string = new String(array);
public class Solution {
public int minMutation(String start, String end, String[] bank) {
if (start==null || end==null || start.length()!=end.length()) return -1;
int steps = 0;
char[] mutations = new char[]{'A', 'C', 'G', 'T'};
HashSet<String> validGene = new HashSet<String>();
for (String str : bank) {
validGene.add(str);
}
if (!validGene.contains(end)) return -1;
if (validGene.contains(start)) validGene.remove(start);
Queue<String> q = new LinkedList<String>();
q.offer(start);
while (!q.isEmpty()) {
int size = q.size();
for (int k=0; k<size; k++) {
String cur = q.poll();
for (int i=0; i<cur.length(); i++) {
for (char c : mutations) {
StringBuilder ss = new StringBuilder(cur);
ss.setCharAt(i, c);
String afterMutation = ss.toString();
if (afterMutation.equals(end)) return steps+1;
if (validGene.contains(afterMutation)) {
validGene.remove(afterMutation);
q.offer(afterMutation);
}
}
}
}
steps++;
}
return -1;
}
}
Leetcode: Minimum Genetic Mutation的更多相关文章
- [LeetCode] Minimum Genetic Mutation 最小基因变化
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- 【LeetCode】433. Minimum Genetic Mutation 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...
- 【leetcode】433. Minimum Genetic Mutation
题目如下: 解题思路:我的思路很简单,就是利用BFS方法搜索,找到最小值. 代码如下: class Solution(object): def canMutation(self, w, d, c, q ...
- [Swift]LeetCode433. 最小基因变化 | Minimum Genetic Mutation
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- 1244. Minimum Genetic Mutation
描述 A gene string can be represented by an 8-character long string, with choices from "A", ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
随机推荐
- Android -- 简单广播接收与发送(2)--动态注册广播接收器
1. 效果图
- javascrit2.0完全参考手册(第二版) 第2章第4节 基本的数据类型
每一个变量都有一个确定的类型表明它存储什么样的数据.js基本的数据类型有strings字符串.numbers数字.Booleans布尔类型.字符串是使用双引号或单引号包含的一串字符:数字包括整数或浮点 ...
- for循环练习
1.输入一个整数,计算从1加到这个数的结果int sum = 0;Console.WriteLine("请输入一个正整数");int a = int.Parse(Console.R ...
- scala - Map基础
Map 构造Map 不可变: val map = Map("sa" -> 1, "s" -> 2)map("sa") = 3 / ...
- Javascript 编程小技巧总结(部分内容借鉴他人)
1 – 使用===,而不是== ==(或!=)操作符在需要的时候会自动执行类型转换.===(或!==)操作不会执行任何转换.它将比较值和类型,而且在速度上也被认为优于==. 2 – 使用闭包实现私有变 ...
- css文本溢出省略号
.ellip{ display: block; width:200px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; ...
- oracle函数,查询,事务
函数包括:单行函数,多行函数(分组函数) 数值函数: --绝对值 select abs(-12.3) from dual; --向上取值 select ceil(5.3) from dual; --向 ...
- python 语料处理(从文件夹中读取文件夹中文件,分词,去停用词,去单个字)
# -*- coding:utf8 -*- import os import jieba def splitSentence(inputFile): fin = open(inputFile, 'r' ...
- BizTalk开发系列(三十五) TCP/IP 适配器
BizTalk 的TCP/IP适配器最初是为英国的保健行业开发.该适配器属于BizTalk进程内适配器,将消息通过TCP/IP 套接字符串在BizTalk服务器与远程客户端间进行通讯. TCP/IP适 ...
- 【转】Gvim开发环境配置笔记--Windows篇
配置文件(vimrc) set nocompatible set nu! set cursorline colorscheme murphy " vim 自身命令行模式智能补全 set wi ...