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 ...
随机推荐
- 异步任务(AsyncTask)
1.Android UI组件更新简介 Android的UI线程主要负责处理用户的按键事件.用户触屏事件及屏幕绘图事件等,因此开发者的其它操作不应该,也不能阻塞UI线程,否则UI界面将会变的停止响应.A ...
- 抛弃jQuery 深入原生的JavaScript
虽然我已经做网站建设工作10多年了,但我从最近3年才开始更多地学习如何更好的将纯JavaScript用于工作中,而不总是将jQuery考虑在第一位.现在我每天学习很多东西.这个过程让我觉得Adtile ...
- NGUI无法按住鼠标按住时无法监听OnHover事件
UICamera.cs 修改前: if ((!isPressed) && highlightChanged) { currentScheme = ControlScheme.Mouse ...
- IE6中常见兼容性问题及浏览器显示难题
1.双倍边距Bug 问题描述:假如有一个ul,里面有若干li,当li设置为左浮动时,此时设置li的margin-left为10px,会在最左侧呈现双倍情况.即20px 正常显示: IE6显示: 修正方 ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- SGU 113
113. Nearly prime numbers time limit per test: 0.25 sec. memory limit per test: 4096 KB Nearly prime ...
- android自动化环境搭建
android自动化环境安装指南 1.appium相关安装(eclipse下)见http://www.cnblogs.com/wangcp-2014/p/5717589.html参考selenium的 ...
- MongoDB (七) MongoDB 数据类型
MongoDB支持许多数据类型的列表下面给出: String : 这是最常用的数据类型来存储数据.在MongoDB中的字符串必须是有效的UTF-8. Integer : 这种类型是用来存储一个数值.整 ...
- interviewbit :Min Steps in Infinite GridBookmark Suggest Edit
You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x ...
- iOS开发--即时通讯常用第三方库
前言 自毕业到现在,从事iOS即时通讯开发已经1年半之久.主要负责Allure开发,目前已上架,可以在苹果商店搜素Allure.Allure模仿微信的交互和设计效果,已经实现微信的大部分功能. 在这里 ...