leetcode:Word Ladder
来源:互联网 发布:软件性能指标有哪些 编辑:程序博客网 时间:2024/06/05 06:10
广度优先搜索,求出从源字符串到目标字符串的变换过程,每一步变换只能是更改其中一个字符串
public class Solution { public int ladderLength(String start, String end, HashSet<String> dict) { if (dict.size() == 0) return 0; LinkedList<String> wordQueue = new LinkedList<String>(); LinkedList<Integer> distanceQueue = new LinkedList<Integer>(); wordQueue.add(start); distanceQueue.add(1); while(!wordQueue.isEmpty()){ String currWord = wordQueue.pop(); Integer currDistance = distanceQueue.pop(); if(currWord.equals(end)){ return currDistance; } for(int i=0; i<currWord.length(); i++){ char[] currCharArr = currWord.toCharArray(); for(char c='a'; c<='z'; c++){ currCharArr[i] = c; String newWord = new String(currCharArr); if(dict.contains(newWord)){ wordQueue.add(newWord); distanceQueue.add(currDistance+1); dict.remove(newWord); } } } } return 0; }}
0 0
- [Leetcode][python]Word Ladder/Word Ladder II
- [Leetcode] Word Ladder
- LeetCode: Word Ladder
- leetcode 84: Word Ladder
- [leetcode] word ladder
- 【leetcode】Word Ladder
- 【leetcode】Word Ladder II
- LeetCode - Word Ladder
- LeetCode之Word Ladder
- [LeetCode]Word Ladder
- [LeetCode]Word Ladder II
- [Leetcode]Word Ladder
- [leetcode]Word Ladder
- [leetcode] Word Ladder
- [leetcode] Word Ladder II
- LeetCode-Word Ladder
- LeetCode- Word Ladder
- LeetCode - Word Ladder II
- GlusterFs文件系统
- 学习《Linux设备模型浅析之设备篇》笔记(深挖二)
- 主函数中的局部变量定义时容易出现的问题
- centos6 can't mount ntfs
- arp病毒系列——攻击类型
- leetcode:Word Ladder
- linux下特殊的进程号
- HDU 1661 Assigments 贪心法题解
- [Unity3D]脚本中Start()和Awake()的区别
- 计算机学院研发------考核之界面
- (CronJob)命令行直接调用CodeIgniter中/controller/action
- 无向图的割顶与桥
- Mac下套件管理器——Homebrew
- 期末前的彷徨