- 浏览: 613223 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
月光杯:
问题解决了吗?
Exceptions in HDFS -
iostreamin:
神,好厉害,这是我找到的唯一可以ac的Java代码,厉害。
[leetcode] word ladder II -
standalone:
One answer I agree with:引用Whene ...
How many string objects are created? -
DiaoCow:
不错!,一开始对这些确实容易犯迷糊
erlang中的冒号 分号 和 句号 -
standalone:
Exception in thread "main& ...
one java interview question
Q: Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
http://leetcode.com/onlinejudge#question_127
Solution:
Much more easier than word ladder 2. BFS search.
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Note:
Return 0 if there is no such transformation sequence.
All words have the same length.
All words contain only lowercase alphabetic characters.
http://leetcode.com/onlinejudge#question_127
Solution:
Much more easier than word ladder 2. BFS search.
public class Solution { public int ladderLength(String start, String end, HashSet<String> dict) { // Start typing your Java solution below // DO NOT write main() function dict.add(start); dict.add(end); // calcualte ajacent matrix HashMap<String, ArrayList<String>> adj = new HashMap<String, ArrayList<String>>(); for(String str : dict){ char [] chars = str.toCharArray(); for(int i=0;i<start.length();i++){ char old = chars[i]; for(char c = 'a' ; c <= 'z'; c++){ if(c == old) continue; chars[i] = c; String newstr = new String(chars); if(dict.contains(newstr)){ ArrayList<String> neighbours = adj.get(str); if(neighbours == null){ neighbours = new ArrayList<String>(); neighbours.add(newstr); adj.put(str, neighbours); }else { neighbours.add(newstr); } } } chars[i] = old; } } HashSet<String> visited = new HashSet<String>(); LinkedList<Node> queue = new LinkedList<Node>(); queue.add(new Node(1, start)); visited.add(start); int pathLen = 0; while(!queue.isEmpty()){ Node n = queue.pollFirst(); if(n.str.equals(end)){ pathLen = n.level; break; }else { ArrayList<String> neighbours = adj.get(n.str); if(neighbours == null || neighbours.isEmpty()) continue; for(String s : neighbours){ if(! visited.contains(s)){ Node p = new Node(n.level+1, s); queue.add(p); visited.add(s); } } } } return pathLen; } public class Node { public int level; public String str; Node(int l, String s){ str = s; level = l; } } }
发表评论
-
ssl 与 java 实例
2014-01-27 10:10 855http://www.iteye.com/topic/1125 ... -
Java NIO
2014-01-10 21:28 764看了这个java nio的教程,明白了什么是Selector. ... -
再谈Java的wait(), sleep(), notify()和notifyAll()
2013-07-25 10:59 1963一段时间不用java,这些概念就全混淆了,有必要彻底澄清一下, ... -
Why singleton is anti-pattern?
2013-07-03 10:12 915OO Test Other reasons? -
How to generate the serialVersionUID when you implement Serializable interface,j
2013-07-01 10:52 987http://docs.oracle.com/javase/6 ... -
Java Override的两个问题
2013-06-01 11:40 10051: 如果子类中的方法的参数是父类的方法的子类型,那么算不算o ... -
Java常用类API统计
2013-06-01 11:35 0String charAt(int) compareTo( ... -
How many string objects are created?
2013-06-01 10:18 1361This is a very common java inte ... -
使用Java的DelayQueue容易碰到的一个坑
2013-05-27 17:32 6814今天不忙,学习一下java.util.concurrent.D ... -
[leetcode] Decode ways
2013-05-02 21:47 1436http://leetcode.com/onlinejudge ... -
[leetcode] Balanced Binary Tree
2013-04-28 14:08 1617Check if a binary tree is balan ... -
[leetcode] find median of two sorted arrays
2013-04-26 10:55 1503http://leetcode.com/onlinejudge ... -
[leetcode] sqrt(x)
2013-04-15 07:44 2483I have talked about this questi ... -
[leetcode] word ladder II
2013-04-15 07:35 12108http://leetcode.com/onlinejudge ... -
[leetcode] Count and Say
2013-04-12 14:05 2282http://leetcode.com/onlinejudge ... -
Date/Time处理函数总结 [To Do]
2013-04-12 10:46 724几种我所用到的用来处理日期,时间的函数总结。 Perl 1 ... -
[leetcode] Palindrome Partition
2013-04-12 10:25 1347http://leetcode.com/onlinejudge ... -
[leetcode] Palindrome Partitioning II
2013-04-11 16:45 1556http://leetcode.com/onlinejudge ... -
Profiling your Java code using Spring
2013-03-05 15:02 734Quite good article!!! http://w ... -
Java的Generics的几点限制
2012-12-28 15:00 4816参见 http://docs.oracle.com/ ...
相关推荐
126.Word_Ladder_II_词语阶梯二【LeetCode单题讲解系列】
python python_leetcode题解之127_Word_Ladder
python python_leetcode题解之126_Word_Ladder_II
javascript js_leetcode题解之127-word-ladder.js
经典字梯游戏c++代码,经测试通过的哦,leetcode上面的题目
javascript js_leetcode题解之126-word-ladder-ii.js
* Word Ladder:该题目要求将一个单词转换成另一个单词,实现方法使用了广度优先搜索算法。 二、树和图 * Median of Two Sorted Arrays Java:该题目要求找到两个排序数组的中位数,实现方法使用了二分查找算法。 ...
在LeetCode上,单词接龙类问题可能表现为“Anagrams”(同字母异序词)或者“Word Ladder”(单词阶梯),这些题目要求高效地搜索满足条件的单词序列。 【压缩包子文件的文件名称列表】"LeetCode-main"可能包含了一...
leetcode 多少题带有堆栈和队列的字梯 您将针对 Lewis Carroll ...在本作业中,您将实现一个函数word_ladder来生成这些字梯。 有许多可能的算法来生成字梯,但一个简单的算法使用堆栈和队列的组合,如
1. leetCode-126-Word-LadderII.md:这涉及到第126题,词梯问题,通常是一个字符串转换问题,目标是找到两个单词之间的最短转换序列,每次只改变一个字母。 2. leetcode-224-Basic-Calculator.md:这是第224题,基础...
6. Word Ladder 单词梯是一个字符串问题,要求将一个单词转换为另一个单词,并且每次转换只能改变一个字符。可以使用广度优先搜索或深度优先搜索来解决该问题。 7. Median of Two Sorted Arrays in Java 两个排序...
5. **搜索和回溯**:对于“NumberofIslands”、“WordLadder”等题目,应聘者必须运用搜索算法,如广度优先搜索(BFS)、深度优先搜索(DFS)以及回溯算法来寻找解决方案。 6. **动态规划和贪心算法**:...
leetcode 316 LeetCode Summary Exclusive Time of Functions: 栈 Friend Circles:DFS Print Binary Tree:二叉树 Maximal Square:DP Maximal Rectangle:单调栈(Histogram变形) Largest Rectangle in Histogram:...
8. **Word Ladder II**:这是一个词链问题,涉及到广度优先搜索和回溯法,寻找从一个单词转换到另一个单词的最短路径。 9. **Regular Expression Matching**:这个涉及到字符串匹配和动态规划。实现一个函数来判断...
9.1 单词变换路径(Word Ladder) 9.1.1 是否存在变换路径 9.1.2 所有最短变换路径9.2 包围区域 10. 深度优先搜索 10.1 N皇后问题 10.2 恢复IP地址 10.3 集合元素之和 10.3.1 元素可以重复 10.3.2 元素不可重复 ...
5 Word Ladder 27 6 Word Ladder II 29 7 Median of Two Sorted Arrays 33 8 Kth Largest Element in an Array 35 9 Wildcard Matching 37 10 Regular Expression Matching in Java 39 11 Merge Intervals 43 12 ...
力扣 Python 解决方案Leetcode 是准备技术编码面试的...特别案例: Word Ladder II,我的 AC 代码比这个 repo 中的代码慢得多。 但是这个 repo 中的一个得到了 TLE,不知道为什么。 任何想法都将受到高度赞赏。 谢谢!
Word Ladder **知识点:** - **问题描述:** - 找到由开始到结尾的字符串的转换字符串集合,中间的转换字符串都要在给定的列表中,并且每一步只能改变一个字符。 - **解决方案分析:** - **原始方法:** - ...
例如,`WordLadder.java`可能涉及到词链问题,需要理解图的邻接矩阵或邻接表表示。 6. **字符串处理**:如模式匹配、字符串反转、最长公共前缀等。`ValidPalindrome.java`可能涉及字符串的比较和操作。 7. **位...
java lru leetcode ...Ladder Add Two Numbers Matrix Spiral Matrix Mergesort [Algorithm Swap](Mergesort/Algorithm swap.md) Numbers Queue Stack Toposort Trie Tree Two Pointers Union Find