zoukankan      html  css  js  c++  java
  • 127单词接龙 1· Word Ladder1

    找出最短路径

    [抄题]:

    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 transformed word must exist in the word list. Note that beginWord is not a transformed word.

    For example,

    Given:
    beginWord = "hit"
    endWord = "cog"
    wordList = ["hot","dot","dog","lot","log","cog"]

    As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
    return its length 5.

    [思维问题]:

    不知道怎么处理字符串:在字典中搜索即可

    [一句话思路]:

    BFS求最短即可

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    1. nextWord.equals(end),如果nextWord到了头,直接返回数组长度length
    2. s.toCharArray();可以把字符串转为字符,拆成很多字符后char要加s

    [二刷]:

    1. 往dict哈希表中添加数组要用简写
    2. 首尾单词相等时返回1,用1表示不需要变
    3. queue hash都需要先把第一个字符串加进去, word和末尾相等时返回length, 下一个单词用nextWord区别
    4. 主函数加个默认返回值=0
    5. nextWords可能有很多,需要新建字符串数组 如果c和第i位字母相同,就不用换了
    6. Queue要用链表Linkedlist来实现
    7. BFS需要先把queue.size()单独取出来
    8. length++;应该在i循环以外,防止循环内不合格 多加1

    [三刷]:

    1. word和末尾相等时返回length(否则length都不会变),需要新建字符串数组 如果c和第i位字母相同,就不用换了
    2. 初始参数dict中没有start end,要自己加

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    1.  单个字符串取长度用的是word.length(); 不是.size()

    [总结]:

    用BFS, 规范书写 考虑所有特殊情况

    [复杂度]:Time complexity: O(边+点) Space complexity: O(边+点)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

    433. Minimum Genetic Mutation 一模一样的吧

     [代码风格] :

    String要大写,java是大小写敏感

    public class Solution {
        public int ladderLength(String start, String end, List<String> wordList) {
            Set<String> dict = new HashSet<>();
            for (String word : wordList) {
                dict.add(word);
            }
            
            if (start.equals(end)) {
                return 1;
            }
            
            HashSet<String> hash = new HashSet<String>();
            Queue<String> queue = new LinkedList<String>();
            queue.offer(start);
            hash.add(start);
            
            int length = 1;
            while (!queue.isEmpty()) {
                length++;
                int size = queue.size();
                for (int i = 0; i < size; i++) {
                    String word = queue.poll();
                    for (String nextWord: getNextWords(word, dict)) {
                        if (hash.contains(nextWord)) {
                            continue;
                        }
                        if (nextWord.equals(end)) {
                            return length;
                        }
                        
                        hash.add(nextWord);
                        queue.offer(nextWord);
                    }
                }
            }
            
            return 0;
        }
    
        // replace character of a string at given index to a given character
        // return a new string
        private String replace(String s, int index, char c) {
            char[] chars = s.toCharArray();
            chars[index] = c;
            return new String(chars);
        }
        
        // get connections with given word.
        // for example, given word = 'hot', dict = {'hot', 'hit', 'hog'}
        // it will return ['hit', 'hog']
        private ArrayList<String> getNextWords(String word, Set<String> dict) {
            ArrayList<String> nextWords = new ArrayList<String>();
            for (char c = 'a'; c <= 'z'; c++) {
                for (int i = 0; i < word.length(); i++) {
                    if (c == word.charAt(i)) {
                        continue;
                    }
                    String nextWord = replace(word, i, c);
                    if (dict.contains(nextWord)) {
                        nextWords.add(nextWord);
                    }
                }
            }
            return nextWords;
        }
    }
    View Code

    找出所有方案中的最短方案

    [抄题]:

    [思维问题]:

    所有-最短:太多

    最短-所有:好找

    [一句话思路]:

    从起点出发BFS求最短,再从终点出发进行DFS求所有方案中有谁最短

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    要求每个点到终点的最短距离,不是每个点到起点的最短距离。应选A。

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    [复杂度]:Time complexity: O(nlgn for bfs) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    BFS求最短

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

  • 相关阅读:
    leetcode[145]Binary Tree Postorder Traversal
    leetcode[146]LRU Cache
    leetcode[147]Insertion Sort List
    leetcode[148]Sort List
    Intro to WebGL with Three.js
    Demo: Camera and Video Control with HTML5
    js ar
    Jingwei Huang
    Tinghui Zhou
    MODS: Fast and Robust Method for Two-View Matching
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8417926.html
Copyright © 2011-2022 走看看