• [leetcode] Word Ladder


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

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

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

    思路:BFS求最短路径。队列弹出字符串,每次变换一个字母,如果在字典中则插入队列,并从字典中删除。插入null来分层。

    import java.util.HashSet;
    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Set;
    
    public class Solution {
        public int ladderLength(String start, String end, Set<String> dict) {
            Queue<String> queue = new LinkedList<String>();
            dict.add(end);
            queue.add(start);
            queue.add(null);
            int len = start.length();
            int res = 1;
    
            while (!queue.isEmpty()) {
                String cur = queue.remove();
    
                if (cur != null && cur.equals(end)) {
                    return res;
                }
                if (cur != null) {
                    for (int i = 0; i < len; i++) {
                        char[] str = cur.toCharArray();
                        for (int j = 0; j < 26; j++) {
                            char ch = (char) ('a' + j);
                            if (str[i] != ch) {
                                str[i] = ch;
                                String changed = new String(str);
                                if (dict.contains(changed)) {
                                    queue.add(changed);
                                    dict.remove(changed);
                                }
                            }
                        }
    
                    }
                } else if (!queue.isEmpty()) {
                    res++;
                    queue.add(null);
                }
            }
            return 0;
    
        }
    
        public static void main(String[] args) {
            Set<String> set = new HashSet<String>();
            String[] a = { "hot", "dot", "dog", "lot", "log" };
            for (String each : a)
                set.add(each);
            System.out.println(new Solution().ladderLength("hit", "log", set));
        }
    }

     第二遍记录:

      bfs的题目,注意null的用法,注意细节。

     

    第三遍记录:

      考虑各种边界情况,比如set是空的(end一开始会加进去,就不空了)

    public class Solution {
        public int ladderLength(String start, String end, Set<String> dict) {
            dict.add(end);
            Queue<String> queue = new LinkedList<String>();
            queue.add(start);
            queue.add(null);
            int step = 1;
            int len =start.length();
            
            while(!queue.isEmpty()){
                String out = queue.remove();
                if(out==null){
                    step++;
                    if(!queue.isEmpty())
                        queue.add(null);        
                }else{
                    if(out.equals(end)){
                        return step;
                    }
                    for(int i=0;i<len;i++){
                        char[] tmp = out.toCharArray();
                        for(int j=0;j<26;j++){
                            tmp[i]=(char)('a'+j);
                            String newStr= new String(tmp);
                            if(dict.contains(newStr)){
                                dict.remove(newStr);
                                queue.add(newStr);
                            }
                            
                        }          
                    }    
                
                }
                
            
            }
            return 0;
            
        }
    
    }

     

  • 相关阅读:
    软件体系结构基本概念汇总
    开发,从需求出发 &#183; 之三 春天在哪里
    chrome模拟手机功能
    Codeforces 2
    ViewPager 入门一
    软考之路--最后的最后,我们都是天使
    分享一段php获取随意时间的前一天代码
    [App Store Connect帮助]七、在 App Store 上发行(2.5)设定价格与销售范围:向企业或教育机构分发您的 App
    [App Store Connect帮助]七、在 App Store 上发行(2.3)设定价格与销售范围:为您的 App 选择地区
    [App Store Connect帮助]七、在 App Store 上发行(2.4)设定价格与销售范围:安排价格调整
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3823472.html
Copyright © 2020-2023  润新知