Leetcode dictionary tree -720: the longest word in the dictionary

The first topic leetcode do, although to do is water problem, but the rookie too much food, just getting started, here to record some basic knowledge. Gangster please see pass by.

https://leetcode-cn.com/problems/longest-word-in-dictionary/

Here is the code with my analysis:

1  class Solution {
 2  public :
 3  
4      class Tree {
 5      public : // here after calling need for clear, otherwise it is recognized as a Tree of private, can not be called
 6          BOOL has_words = false ;
 7          the Vector <Tree *> v ;
 . 8  
. 9          Tree (): has_words ( to false ), V ( 26 is ) {};
 10          // initialization parameter when the transmission is actually v 26 produces an array on the vector 
. 11          static  void wordsinsert (Tree T *, String AIM) {
 12 is              for ( char C: AIM) {
 13 is                 if(!t->v[c-'a'])t->v[c-'a']=new Tree;
14                 t=t->v[c-'a'];
15             }
16             t->has_words=true;
17         }
18 
19         static bool searchwords(Tree* t,string aim){
20             for(char c:aim){
21                 t=t->v[c-'a'];
22                 if(! T-> has_words) return  to false ;
 23 is              }
 24              return  to true ;
 25          }
 26 is      };
 27  
28      String longestWord (Vector < String > & words) {
 29          Tree * = the root new new Tree;
 30          for ( String AIM: words) {// Note methods familiar traverse
 31 is              Tree :: wordsinsert (the root, AIM); // Tree because the two functions are declared after the static type, so here means the type name in the absence of type instances corresponding direct call function
 32          } // Details about the static function resolved (be sure to look carefully!)
 33          String longest ="";
34         for(string aim:words){
35             if(Tree::searchwords(root,aim)){
36                 if(aim.size()>longest.size())longest=aim;
37                 else if(aim.size()==longest.size()&&aim<longest)longest=aim;
38             }//字典序直接比较即可
39         }
40         return longest;
41     }
42 };

OK

Guess you like

Origin www.cnblogs.com/savennist/p/12377831.html