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 ...
随机推荐
- [工作积累] Android: Hide Navigation bar 隐藏导航条
https://developer.android.com/training/system-ui/navigation.html View decorView = getWindow().getDec ...
- .NET设计模式(7):创建型模式专题总结(Creational Pattern)(转)
概述 创建型模式,就是用来创建对象的模式,抽象了实例化的过程.它帮助一个系统独立于如何创建.组合和表示它的那些对象.本文对五种常用创建型模式进行了比较,通过一个游戏开发场景的例子来说该如何使用创建型模 ...
- Confluence Wiki Markup & Markdown
Markup : 默认有支持 Markdown : 需先安装插件,插件下载地址: Confluence markdown : https://marketplace.atlassian.com/plu ...
- associated 2 maps
<!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content= ...
- ibatis的iterate使用
Iterate:这属性遍历整个集合,并为 List 集合中的元素重复元素体的内容. Iterate 的属性: prepend - 可被覆盖的 SQL 语句组成部分,添加在语句的前面(可选 ...
- POJ 1317
#include <iostream> #include <string> using namespace std; char p_code[] = {'_','a','b', ...
- HDU 1385 Minimum Transport Cost (Dijstra 最短路)
Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...
- zoj 3529 A Game Between Alice and Bob 博弈论
思路:每个数的SG值就是其质因子个数,在进行nim博弈 代码如下: #include<iostream> #include<cstdio> #include<cmath& ...
- 高性能js之js加载执行
转载自:http://www.blogjava.net/BearRui/archive/2010/04/08/web_performance_js_where.html 外部JS的阻塞下载 所有浏览器 ...
- socket异步编程--libevent的使用
使用 libevent 和 libev 提高网络应用性能 http://www.ibm.com/developerworks/cn/aix/library/au-libev/ libevent实现ht ...