zoukankan      html  css  js  c++  java
  • 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:
    
    Only one letter can be changed at a time
    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.
    

      DFS 小数据AC:

    class Solution {
    public:
       bool check(const string & a, const string &b)
        {
            int num = 0;
            if(a.size() != b.size()) return false;
            for(int i = 0; i< a.size() ; i++)
            {
                if(a[i] != b[i])
                   num++;
            }
            return num == 1;
        }
        void DFS(const string &start, const string &end, unordered_set<string> &dict, vector<bool> &flag, int nums){
        
                if(start == end ){
                    res = res > nums ? nums : res;
                    return ;
                }
                int i;auto it = dict.begin();
               for( i= 0; it != dict.end(); it++,i++) 
                   if(flag[i] == false && check(start,*it))
                   {
                        flag[i] = true;
                        DFS(*it,end,dict, flag, nums+1);
                        flag[i] = false;
                   }
        
        }
        int ladderLength(string start, string end, unordered_set<string> &dict) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
              res = dict.size() + 1 ;
              vector<bool> flag(dict.size(), false);
              DFS(start, end, dict, flag, 0);
              if(res  ==  dict.size() + 1) return 0;
              return res +1 ;
        }
    private : 
      int res;
    };

     BFS: 过大数据

    class Solution {
    public:
        int ladderLength(string start, string end, unordered_set<string> &dict) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(start.size() != end.size()) return 0;
            if(dict.size() == 0) return 0;
            
            queue<string> myqueue, myqueueT;
            myqueue.push(start);
            int depth = 1;
            
            while(!myqueue.empty()){
              depth++;
              while(!myqueue.empty()){
                string str = myqueue.front();
                myqueue.pop();
                for(int i = 0; i < str.size() ; i++){
                    char temp = str[i] ;
                    for(char c = 'a'; c <= 'z' ;c++){
                         if(c == temp) continue;
                         str[i] = c;
                         if(str == end) return depth;
                         auto it = dict.find(str) ;
                         if(it != dict.end() ){
                            myqueueT.push(str);
                            dict.erase(it);
                         }
                    }
                    str[i] = temp;
                }
              }
              myqueue.swap( myqueueT);
            }
            //don't find 
            return 0;
        }
    };
  • 相关阅读:
    打造TypeScript的Visual Studio Code开发环境
    Visual Studio Code 使用Git进行版本控制
    每周一书《Oracle 12 c PL(SQL)程序设计终极指南》
    每周一书-《鸟哥的Linux私房菜》获奖公布
    Python黑帽编程 4.1 Sniffer(嗅探器)之数据捕获(上)
    微信公众平台及门户应用
    Bootstrap开发框架
    公用类库使用帮助
    git
    SQL Server 基本函数 与 类型
  • 原文地址:https://www.cnblogs.com/graph/p/3237214.html
Copyright © 2011-2022 走看看