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 ...
随机推荐
- SQL中自定义拆分为新表的函数
/*按照符号分割字符串*/ create function [dbo].[m_split](@c varchar(2000),@split varchar(2)) returns @t table(c ...
- imageNamed 与 imageWithContentsOfFile的区别
如题,是不是大家为了方便都这样加载图片啊 myImage = [UIImage imageNamed:@"icon.png"];那么小心了这种方法在一些图片很少,或者图片很小的程序 ...
- mysql存储过程和事件
1.会员表member和车辆表car,更新每个会员下面的车辆数量have_car字段. DELIMITER $$ USE $$ DROP PROCEDURE IF EXISTS `sp_update_ ...
- PHP合并数组array_merge函数运算符加号与的区别
两个的区别是:1.数组键名为数字键名时,要合并的两个数组中有同名数字KEY的时候,使用array_merge()不会覆盖掉原来的值,而使用“+”合并数组则会把最先出现的值作为最终结果返回,而把后面的数 ...
- POJ 3696 The Luckiest number (欧拉函数,好题)
该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...
- HDU 1978 How many ways(动态规划)
How many ways http://acm.hdu.edu.cn/showproblem.php?pid=1978 Problem Description 这是一个简单的生存游戏,你控制一个机器 ...
- iOS多线程的初步研究(五)-- 如何让NSURLConnection在子线程中运行
可以有两个办法让NSURLConnection在子线程中运行,即将NSURLConnection加入到run loop或者NSOperationQueue中去运行. 前面提到可以将NSTimer手动加 ...
- procedure can't return a result set in the given context
调用存储过程失败!出现如下错误:PROCEDURE ipbx.qu_ery can't return a result set in the given context, ipbx是数据库, qu_e ...
- m_pMainWnd(转载)
今天写程序时遇到个简单而又很有意思的问题,封装了一个网络接口类,发送数据以及网络的回调接口都在这个类里面,打算在回调函数里给AfxGetMainWnd()发送消息以更新主界面的数据,同时程序有一个登录 ...
- http://blog.csdn.net/woshiyjk/article/details/7895888
http://blog.csdn.net/woshiyjk/article/details/7895888