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:

    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.

    Solution:

    我自己的做法,大数据时会超时:

    利用两个queue:

    1个queue用来存放本层的单词,一个queue用来存放下一层的单词。

    当本层的单词用完了以后,再将下一层的单词变成本层的就好,下一层清空。

    注意,java的object是pass by reference的,得重新new一下才行。

     1 public int ladderLength(String start, String end, Set<String> dict) {
     2         if(start==null||end==null||dict==null||dict.size()==0){
     3             return 0;
     4         }
     5         Queue<String> curQueue=new LinkedList<String>();
     6         Queue<String> nextQueue=new LinkedList<String>();
     7         curQueue.offer(start);
     8         int count=1;
     9         if(dict.contains(start))
    10             dict.remove(start);
    11         while(!curQueue.isEmpty()){              
    12             if(curQueue.peek().equals(end))
    13                 return count;
    14             String haha=curQueue.poll();            
    15             for(int i=0;i<start.length();++i){
    16                 char[] temp_c=haha.toCharArray();
    17                 char m=temp_c[i];
    18                 for(char j='a';j<='z';++j){
    19                     if(m==j)
    20                         continue;
    21                     temp_c[i]=j;
    22                     String temp=new String(temp_c);  
    23                     //System.out.println("size o Cur Queue==="+temp);
    24                     if(dict.contains(temp)){
    25                         
    26                         dict.remove(temp);
    27                         nextQueue.offer(temp);
    28                     }
    29                 }
    30             }
    31             if(curQueue.isEmpty()){
    32                 curQueue=new LinkedList<String>(nextQueue);
    33                 nextQueue.clear();
    34                 count++;
    35             }
    36         }
    37         return 0;
    38     }

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

    网上看到的做法:

    不用两个queue来分别存放上下两层的数据,只用一个queue来实现就好,但是分别用两个counter来计数,上层有多少个words,下层有多少个words。同理,当上层的words都从queue里poll出去了以后,上层counter=下层counter,下层counter清空。

     1 public class Solution {
     2     public int ladderLength(String start, String end, Set<String> dict) {
     3         if(start==null||end==null||dict==null||dict.size()==0)
     4             return 0;
     5         Queue<String> queue=new LinkedList<String>();
     6         queue.offer(start);
     7         int curNum=1;
     8         int nextNum=0;
     9         int level=1;
    10         while(!queue.isEmpty()){
    11             if(queue.peek().equals(end)){
    12                 return level;
    13             }
    14             curNum--;
    15             String temp=queue.poll();
    16             for(int i=0;i<temp.length();++i){
    17                 char[] c=temp.toCharArray();
    18                 for(char j='a';j<='z';++j){
    19                     c[i]=j;
    20                     String sc=new String(c);
    21                     // if(sc.equals(end)){
    22                     //     return level;
    23                     // }
    24                     if(dict.contains(sc)){
    25                         dict.remove(sc);
    26                         nextNum++;
    27                         queue.offer(sc);
    28                     }
    29                 }
    30             }
    31             if(curNum==0){
    32                 curNum=nextNum;
    33                 nextNum=0;
    34                 level++;
    35             }
    36         }
    37         return 0;
    38     }
    39 }
  • 相关阅读:
    ajax学习之1-经典登陆验证
    jquery学习之1.22-小练习5-实现添加页面上填写信息到当前页面表格中
    jquery学习之1.21-小练习4实现对复选框的全选,全不选,反选
    生活小感想3
    jquery学习之1.20-获取同辈元素和子元素
    jquery学习之1.19-小练习3-输入用户名密码时焦点触发和失去焦点
    jquery学习之1.17-小练习2-左右选项框内容移动
    jquery学习之1.16-替换节点
    Ioc-Autofac实现自动的注入
    Ioc-Autofac的使用
  • 原文地址:https://www.cnblogs.com/Phoebe815/p/4265180.html
Copyright © 2011-2022 走看看