题目还是很好的,提供了一种新的思路方向。

细节方面,开始我的判断条件用的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的更多相关文章

  1. Leetcode: Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  2. [LeetCode] Minimum Genetic Mutation 最小基因变化

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  3. [Swift]LeetCode433. 最小基因变化 | Minimum Genetic Mutation

    A gene string can be represented by an 8-character long string, with choices from "A", &qu ...

  4. 1244. Minimum Genetic Mutation

    描述 A gene string can be represented by an 8-character long string, with choices from "A", ...

  5. 【LeetCode】433. Minimum Genetic Mutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址: https://leetcode. ...

  6. 【leetcode】433. Minimum Genetic Mutation

    题目如下: 解题思路:我的思路很简单,就是利用BFS方法搜索,找到最小值. 代码如下: class Solution(object): def canMutation(self, w, d, c, q ...

  7. [LeetCode] Word Ladder 词语阶梯

    Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. 127单词接龙 1· Word Ladder1

    找出最短路径 [抄题]: Given two words (beginWord and endWord), and a dictionary's word list, find the length ...

  10. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. 从Theano到Lasagne:基于Python的深度学习的框架和库

    从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...

  2. 负MARGIN之讲解

    css中的负边距(negative margin)是布局中的一个常用技巧,只要运用得合理常常会有意想不到的效果.很多特殊的css布局方法都依赖于负边距,所以掌握它的用法对于前端的同学来说,那是必须的. ...

  3. org.apache.kafka.common.network.Selector

    org.apache.kafka.common.client.Selector实现了Selectable接口,用于提供符合Kafka网络通讯特点的异步的.非阻塞的.面向多个连接的网络I/O. 这些网络 ...

  4. ExtJs之进度条实现

    慢慢按书上的作. <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta h ...

  5. [你必须知道的.NET]第三十三回,深入.NET 4.0之,Lazy<T>点滴

    发布日期:2009.10.29 作者:Anytao © 2009 Anytao.com ,Anytao原创作品,转贴请注明作者和出处. 对象的创建方式,始终代表了软件工业的生产力方向,代表了先进软件技 ...

  6. CF 197 DIV2 Xenia and Bit Operations 线段树

    线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...

  7. JAVA类型信息——反射机制

    JAVA类型信息——反射机制 一.反射机制概述 1.反射机制:就是java语言在运行时拥有的一项自我观察的能力,java通过这种能力彻底了解程序自身的情况,并为下一步的动作做准备. 2.反射机制的功能 ...

  8. spring_150806_hibernate_non_transaction

    添加hibernate的相关jar包! 实体类: package com.spring.model; import javax.persistence.Entity; import javax.per ...

  9. C/C++ 位域知识小结

    C/C++ 位域知识小结 几篇较全面的位域相关的文章: http://www.uplook.cn/blog/9/93362/ C/C++位域(Bit-fields)之我见 C中的位域与大小端问题 内存 ...

  10. MFC的GUI窗口使用Console输出函数printf(AllocConsole后,使用GetStdHandle取得句柄,然后就可以操作了)

    在GUI程序中使用printf函数: #include <io.h> #include <fcntl.h> void InitConsole() { int nRet= 0; ...