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("################"); }
}
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] Minimum Genetic Mutation 最小基因变化
A gene string can be represented by an 8-character long string, with choices from "A", &qu ...
- [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】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 ...
- [LeetCode] Word Ladder 词语阶梯
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 127单词接龙 1· Word Ladder1
找出最短路径 [抄题]: Given two words (beginWord and endWord), and a dictionary's word list, find the length ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- 安装JDK后环境变量的配置
1.JAVA_HOME:JDK的安装路径2.CLASSPATH:java运行时加载的类路径,即JDK的lib下面的tools.jar和dt.jar3.PATH:JDK安装包下的bin目录
- ASP.NET页面刷新的实现方法总结
先看看ASP.NET页面刷新的实现方法: 第一: private void Button1_Click( object sender, System.EventArgs e ) { Response. ...
- 遭遇Asp.Net长文件名下载的问题和解决办法
在Asp.Net中写了一个附件上传和下载的程序,附件上传到数据库中,然后将附件的GUID保存起来,我们可以根据GUID来找到数据库中的附件,一般附件下载的代码是: <!--<br /> ...
- java基础类:Object类和Math类
1.2.3.4.5.6.7.7.
- leetcode course shedule
题目就不说了,问题本质就是在一个有向图中查找它是不是存在环. 上网百度了一下,方法是,找出图中入度为0 的点,将以它为起点的边去掉. 重复这一动作,直到所有的边都被去掉(没有环)或者存在边但是无法再去 ...
- javascript笔记 面向对象
Javascript是一种面向对象的弱语言,既然有面向对象,就有继承 继承: 1.call函数和apply函数:区别在于它们参数上的不同,固定参数的用call,可变参数的用apply.换句话说,就是a ...
- Logstash最佳实践
http://udn.yyuap.com/doc/logstash-best-practice-cn/index.html
- POJ 1144 Network(Tarjan)
题目链接 题意 : 找出割点个数. 思路 : Tarjan缩点,u是割点的充要条件是:u要么是具有两个以上子女的深度优先生成树的根,要么不是根,而有一个子女v满足low[v]>=dfn[u]. ...
- 传说中的WCF(12):服务器回调有啥用
你说,服务器端回调有啥用呢?这样问,估计不好回答,是吧.不急,先讨论一个情景. 假设现有服务器端S,客户端A开始连接S并调用相关操作,其中有一个操作,在功能上有些特殊,调用后无法即时回复,因为在服务器 ...
- JAVA容器
JAVA容器 一.容器体系结构 java.util 二.迭代器Iterator<E> 迭代器是一种设计模式,可以遍历并选择序列中的对象,而开发人员并不需要了解该序列的底层结构.迭代器通常被 ...