File tree Expand file tree Collapse file tree 1 file changed +5
-9
lines changed
design-add-and-search-words-data-structure Expand file tree Collapse file tree 1 file changed +5
-9
lines changed Original file line number Diff line number Diff line change @@ -56,25 +56,21 @@ class WordDictionary {
5656 TrieNode* root;
5757
5858 bool _search (string word, int idx, TrieNode* node) {
59+ if (node == nullptr ) return false ;
60+
5961 if (word.size () == idx) return node->word ;
6062
6163 char c = word[idx];
6264
6365 if (c != ' .' ) {
64- if (node->links [c - ' a' ] == nullptr ) return false ;
65-
6666 TrieNode* next_node = node->links [c - ' a' ];
6767 int next_idx = idx + 1 ;
6868
6969 return _search (word, next_idx, next_node);
7070 } else {
71- for (TrieNode* link : node->links ) {
72- if (link != nullptr ) {
73- TrieNode* next_node = link;
74- int next_idx = idx + 1 ;
75-
76- if (_search (word, next_idx, next_node)) return true ;
77- }
71+ for (TrieNode* next_node : node->links ) {
72+ int next_idx = idx + 1 ;
73+ if (_search (word, next_idx, next_node)) return true ;
7874 }
7975 return false ;
8076 }
You can’t perform that action at this time.
0 commit comments