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 ...
随机推荐
- 基于SVG的JS地图插件
一:D3(Data-Driven Documents) 官网地址:http://d3js.org/ 功能非常强大(不支持IE8) D3 是最流行的可视化库之一,它被很多其他的表格插件所使用.它允许绑定 ...
- Ajax分页js代码
var pageIndex = 0; var pageSize = 10; $(function () { $("#btnSearch").click(function () { ...
- 存储过程: 存储过程(stored procedure)有时也称为sproc。存储过程存储于数据库中而不是在单独的文件中,有输入参数、输出参数以及返回值等。
存储过程示例一: 执行存储过程方法一: 执行存储过程方法二: 存储过程可以定义返回值: 修改存储过程: 利用存储过程查找三个表内的信息: 练习: 超市管理系统:表一:门店仓库表 MenDian ...
- Linux_文件打包,压缩,解压
一.压缩命令 文件格式:*.gz 命令:gzip 文件名 (ps:不能压缩目录,切压缩后不保留原文件) 压缩前 -rw-r--r--. 1 root root 315 Sep 6 21:03 df.t ...
- Linux下配置SVN
1.安装svn yum -y install subversion 2.创建SVN版本库目录 mkdir -p /var/svn/svnrepos (-p参数:如果没有这个目录,则创建这个目录) 3. ...
- 本地数据库(SQL Server)远程连接服务器端服务器
本地数据库(SQL Server 2012) 连接外网服务器的数据库,外网的服务器端需要做如下配置: 1. 首先是要打开 数据的配置管理工具 2. 配置相关的客户端协议,开启TCP/IP 3. 数据库 ...
- strcat、strcpy、memcpy 的使用注意
char *p = "hello";//5 个长度 ; //char *str = (char *)malloc(sizeof(char)*len); ] = "niha ...
- linux笔记六-------文件权限设置
1.文件(目录)权限本身划分:读read 写write 执行execute 权限从用户的角度划分:主人权限user 同组用户权限group 其他组用户的权限other music. ...
- js实现时钟
<!DOCTYPE html> <html> <head> <title>Js版带表盘的时钟</title> <meta charse ...
- mysql批量替换指定字符串
将字段title中的abc替换为123 UPDATE `table1` SET `title` = replace(title, 'abc', '123') WHERE `typeid` =18;