- 浏览: 183497 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.
给定两个单词和一个单词集合,从一个单词变换到另一个单词,每次只能改变一个字母,并且变换后的单词都要在单词集合中,找出最少的变换次数。我们从beginWord开始,每个字符都可能有26种变换,我们用一个set来记录在wordlist中已经被访问过的单词,用path来记录走过的长度,如果变换后的单词与endword相同就返回path+1,如果不相同,看是否在wordlist中并且检查是否被访问过,然后更新beginSet,直到找到一条路,或者beginSet为空,返回0。代码如下:
Only one letter can be changed at a time
Each intermediate word must exist in the word list
For example,
Given:
beginWord = "hit"
endWord = "cog"
wordList = ["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.
给定两个单词和一个单词集合,从一个单词变换到另一个单词,每次只能改变一个字母,并且变换后的单词都要在单词集合中,找出最少的变换次数。我们从beginWord开始,每个字符都可能有26种变换,我们用一个set来记录在wordlist中已经被访问过的单词,用path来记录走过的长度,如果变换后的单词与endword相同就返回path+1,如果不相同,看是否在wordlist中并且检查是否被访问过,然后更新beginSet,直到找到一条路,或者beginSet为空,返回0。代码如下:
public class Solution { public int ladderLength(String beginWord, String endWord, Set<String> wordList) { Set<String> beginSet = new HashSet<String>(); Set<String> isVisited = new HashSet<String>(); beginSet.add(beginWord); int path = 1; while(!beginSet.isEmpty()) { Set<String> tem = new HashSet<String>(); for(String word : beginSet) { char[] c = word.toCharArray(); for(int i = 0; i < c.length; i++) { char oldChar = c[i]; for(char j = 'a'; j <= 'z'; j++) { c[i] = j; String s = new String(c); if(s.equals(endWord)) return path + 1; if(wordList.contains(s) && !isVisited.contains(s)) { tem.add(s); isVisited.add(s); } } c[i] = oldChar; } } beginSet = tem; path ++; } return 0; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
标题中的“Stanford WordLadder”和“Randomwriter”是两个特定的程序或工具,它们在IT领域,尤其是自然语言处理(NLP)方面有一定的应用。让我们分别详细探讨这两个概念。 **Stanford WordLadder** Stanford Word...
经典字梯游戏c++代码,经测试通过的哦,leetcode上面的题目
word_ladder问题源代码 算法导论
126.Word_Ladder_II_词语阶梯二【LeetCode单题讲解系列】
《C#实现BluePrism.WordLadder:探索图算法在解决文字阶梯问题中的应用》 在信息技术领域,算法是解决问题的关键工具,而C#作为一款强大的编程语言,常常被用来实现各种复杂算法。本篇文章将深入探讨一个名为"Blue...
Ladder with authenticating user 1. 实现spring security的四种方法 1. 全部利用配置文件 不使用数据库,全部信息写在配置文件中,如拦截的URL及对应权限,指定用户名、密码和对应权限 2. 数据库+配置文件 数据库...
【标题】"wordLadder_web"是一个基于Web的程序,可能是一个在线版的单词阶梯游戏。单词阶梯游戏,也称为字母阶梯或词链,是一种语言游戏,玩家需要通过改变一个单词中的一个字母来逐渐转换成目标单词,每次变换只能...
"wordladder:JavaScript 代码面试片段"是一个面试场景,旨在考察候选人的代码分析、问题解决和重构能力。在这个问题中,面试官通常会提供一段实现字梯游戏的JavaScript代码,字梯游戏是一种逻辑谜题,玩家需要通过...
WordLadder游戏是一种基于单词的逻辑游戏,最初由著名作家刘易斯·卡罗尔(Lewis Carroll)设计,也被称为“单词阶梯”或“字母梯”。在这个游戏中,玩家需要从一个给定的起始单词出发,通过改变一个字母每次形成一...
"wordLadder422C" 是一个特定的项目名称,它与EE 422C这门课程相关。EE通常代表Electrical Engineering(电子工程),而422C可能是课程编号,这表明这个项目是为电子工程专业学生设计的。"单词阶梯"(Word Ladder)...
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 ...
WordLadder.cpp: Word Ladder Program and Word Changing Utilities SeparateChaining.h: Header file for separate chaining SeparateChaining.cpp: Implementation for separate chaining TestSeparateChaining...
python python_leetcode题解之127_Word_Ladder
- **WordLadder.java**: 词链问题,涉及图论和搜索算法,如广度优先搜索(BFS),通常用于找到两个单词之间通过改变一个字母形成的新单词的最短路径。 - **堆数据结构** (PairingHeap.java): 堆是一种特殊的树形数据...
* Word Ladder:该题目要求将一个单词转换成另一个单词,实现方法使用了广度优先搜索算法。 二、树和图 * Median of Two Sorted Arrays Java:该题目要求找到两个排序数组的中位数,实现方法使用了二分查找算法。 ...
63.java HashFamily.java IntCell.java KdTree.java LeftistHeap.java MaxSumTest.java MemoryCell.java MyArrayList.java MyLinkedList.java ...TestMemoryCell.java Treap.java UnderflowException.java WordLadder.java
javascript js_leetcode题解之127-word-ladder.js
python python_leetcode题解之126_Word_Ladder_II
public class WordLadder { public int ladderLength(String beginWord, String endWord, List<String> wordList) { // 将 wordList 转换为 set,方便查询 Set<String> wordSet = new HashSet(wordList); // ...
5. **搜索和回溯**:对于“NumberofIslands”、“WordLadder”等题目,应聘者必须运用搜索算法,如广度优先搜索(BFS)、深度优先搜索(DFS)以及回溯算法来寻找解决方案。 6. **动态规划和贪心算法**:...