【第22期】观点:IT 行业加班,到底有没有价值?

LeetCode-127.Word Ladder

原创 2016年05月30日 21:13:26

https://leetcode.com/problems/word-ladder/

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:

  1. Only one letter can be changed at a time
  2. 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.

将问题转化为求树的最小深度问题 LeetCode-111.Minimum Depth of Binary Tree

c++

int ladderLength(string beginWord, string endWord, unordered_set<string>& wordList) 
{
	int len = 1, slen = beginWord.length(),n;
	queue<string> q;
	q.push(beginWord);
	while ((n = q.size()) > 0)
	{
		len++;
		for (int i = 0; i < n; i++)
		{
			string s = q.front();
			q.pop();
			for (int j = 0; j < slen; j++)
			{
				char ch = s[j];
				for (char c = 'a'; c <= 'z'; c++)
				{
					s[j] = c;
					if (s == endWord)
						return len;
					if (wordList.find(s) != wordList.end())
					{
						q.push(s);
						wordList.erase(s);
					}
					s[j] = ch;
				}
			}
		}
	}
	return 0;
}


public int LadderLength(string beginWord, string endWord, ISet<string> wordList) 
    {
        int len = 1,slen= beginWord.Length,n;
        Queue<string> q = new Queue<string>();
        q.Enqueue(beginWord);
        while ((n=q.Count)>0)
        {
            len++;
            for (int i = 0; i < n; i++)
            {
                string s = q.Dequeue();
                for (int j = 0; j < slen; j++)
                {
                    char[] ch = s.ToCharArray();
                    for (char c = 'a'; c <= 'z'; c++)
                    {
                        ch[j] = c;
                        string tmp = new string(ch);
                        if (tmp == endWord)
                            return len;
                        if (wordList.Contains(tmp))
                        {
                            q.Enqueue(tmp);
                            wordList.Remove(tmp);
                        }
                    }
                }
            }
        }
        return 0;
    }

参考

http://www.cnblogs.com/TenosDoIt/p/3443512.html

http://blog.csdn.net/linhuanmars/article/details/23029973


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

[leetcode]127. Word Ladder@Java解题报告

https://leetcode.com/problems/word-ladder/#/description Given two words (beginWord and endWord...

[LeetCode]127.Word Ladder

题目 Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that: Only one let...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

[LeetCode]127.Word Ladder

Given two words (start and end), and a dictionary, find the length of shortest transformation sequen...

Leetcode - Word Ladder

Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that: ...

LeetCode127 Word Ladder

详细见:leetcode.com/problems/word-ladder Java Solution: github package leetcode; import java.u...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)