[leetcode]126 Word Ladder II

Source: Internet
Author: User

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

http://blog.csdn.net/linhuanmars/article/details/23071455

public class solution {    public list<list<string>>  Findladders (string start, string end, set<string> dict)      {        list<list<string>> toreturn = new  ArrayList<> ();                 Queue<Node> queue = new LinkedList<> ();         queue.offer (New node (start, new arraylist<string> ()));                 int minlen  = -1;                 Set<String> visited = new HashSet<> ();         visited.add (StART);                 while   (!queue.isempty ())         {             node node = queue.poll ();             if  (Node.str.equals (end))              {                 if  (minlen == -1)                  {                     toreturn.add (Node.path);                     minlen =  Node.path.size ();                 }                 else if  ( Node.path.size ()  == minlen)                  {                     toreturn.add (Node.path);                 }             }            else             {                 visited.add (NODE.STR);              &nbsP;  list<node> nextnodes = next (node, dict, visited);                 for  (node  Nextnode : nextnodes)                      queue.offer (NextNode);             }        }                 return toReturn;     }            private List<Node>  next (node from, set<string> dict, set<string> visited)      {        list<node> toreturn = new  ArrayList<> (&NBSP;&NBS);p;              char[] chars =  from.str.tochararray ();        for  (int i =  0 ; i < chars.length ; i ++)          {            char oric =  chars[i];            for  (char c =   ' A '  ; c <=  ' z '  ; c ++)              {                 if  (c == oric)                      continue;                 chars[i] = c;                 string str = new string (chars);                 if  (Dict.contains (str )  && !visited.contains (str))                  {                     toreturn.add (New node (str, from.path));                 }             }             chars[i] = oric;        }                 return toreturn;    }         private static class Node    {         string str;        list <string> path;        node (String str, List< String> oldpath)         {             this.str = str;             path = new ArrayList<> (OldPath);             path.add (str);         }     }}


[leetcode]126 Word Ladder II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.