leetcode-WordLadder

更多LeetCode算法与参考: 

https://github.com/kkman2008/Notebook/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7%A3.md

-------------------------

leetcode-WordLadder 

题目:
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.

由于只需要求最短的距离,所以直接BFS就行了。这题OJ的数据卡得比较严,裸BFS会TLE。没有必要写双向的BFS,注意下剪枝就可以了。直接放代码。
code:

import java.util.*;

public class WordLadder {
    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        LinkedList<String> queue = new LinkedList<String>();
        
        queue.offer(beginWord);
        map.put(beginWord, 1);
        boolean flag = false;
        while(!queue.isEmpty()){
            String curr = queue.poll();
            for(String next : getNextWords(curr, wordList)){
                if(next.equals(endWord)){
                    flag = true;
                    map.put(next, map.get(curr) + 1);
                    System.out.println(next + " " + map.get(next));
                    return map.get(next);
                }
                if(wordList.contains(next)){
                    if(!map.containsKey(next)){
                        map.put(next, map.get(curr) + 1);
                        System.out.println(next + " " + map.get(next));
                        queue.offer(next);
                    }
                }
            }
        }
        if(flag) return map.get(endWord);
        else return 0;
    }
    
    public List<String> getNextWords(String word, Set<String> wordList){
        List<String> nextWords = new ArrayList<String>();
        for(int i = 0; i < word.length(); i++){
            for(char c = 'a'; c <= 'z'; c++){
                if(c == word.charAt(i)) continue;
                char[] arr = word.toCharArray();
                arr[i] = c;
                String next = new String(arr);
                if(wordList.contains(next)) nextWords.add(next);
            }
        }
        return nextWords;
    }
    
    public static void main(String[] args){
        WordLadder WL = new WordLadder();
        Set<String> wordList = new HashSet<String>();
        wordList.add("a");
        wordList.add("b");
        wordList.add("c");
        String beginWord = "a";
        String endWord = "c";
        int result = WL.ladderLength(beginWord, endWord, wordList);
        System.out.println(result);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值