文章目录
前言
整理力扣刷题思路。
- 语言:python
- 题库:来自neetcode: link
一、预备知识
前缀树
入门教程:link
208.implement-trie-prefix-tree
Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。
请你实现 Trie 类:
- Trie() 初始化前缀树对象。
- void insert(String word) 向前缀树中插入字符串 word 。
- boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
- boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。
link
class Node():
def __init__(self):
self.children = collections.defaultdict(Node)
self.isword = False
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word: str) -> None:
cur = self.root
for w in word:
cur = cur.children[w]
cur.isword = True
def search(self, word: str) -> bool:
cur = self.root
for w in word:
cur = cur.children.get(w)
if not cur:
return False
return cur.isword
def startsWith(self, prefix: str) -> bool:
cur = self.root
for w in prefix:
cur = cur.children.get(w)
if not cur:
return False
return True
# Your Trie object will be instantiated and called as such:
# obj = Trie()
# obj.insert(word)
# param_2 = obj.search(word)
# param_3 = obj.startsWith(prefix)
211.design-add-and-search-words-data-structure
请你设计一个数据结构,支持 添加新单词 和 查找字符串是否与任何先前添加的字符串匹配 。
实现词典类 WordDictionary :
- WordDictionary() 初始化词典对象
- void addWord(word) 将 word 添加到数据结构中,之后可以对它进行匹配
- bool search(word) 如果数据结构中存在字符串与 word 匹配,则返回 true ;否则,返回 false 。word 中可能包含一些 ‘.’ ,每个 . 都可以表示任何一个字母。
link
class Node():
def __init__(self):
self.children = collections.defaultdict(Node)
self.isword = False
class WordDictionary:
def __init__(self):
self.root = Node()
def addWord(self, word: str) -> None:
cur = self.root
for w in word:
cur = cur.children[w]
cur.isword = True
def search(self, word: str) -> bool:
return self.match(word, 0, self.root)
def match(self, word, index, root):
if not root:
return False
if index == len(word):
return root.isword
if word[index] != '.':
return self.match(word, index + 1, root.children.get(word[index]))
else:
for child in root.children.values():
if self.match(word, index+1, child):
return True
return False
# Your WordDictionary object will be instantiated and called as such:
# obj = WordDictionary()
# obj.addWord(word)
# param_2 = obj.search(word)
二、解题思路
穷举
212.word-search-ii
给定一个 m x n 二维字符网格 board 和一个单词(字符串)列表 words, 返回所有二维网格上的单词 。
单词必须按照字母顺序,通过 相邻的单元格 内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
link
class Node:
def __init__(self):
self.children = collections.defaultdict(Node)
self.word = ''
class Solution:
def __init__(self):
self.root = Node()
def insert(self,word):
cur = self.root
for w in word:
cur = cur.children[w]
cur.word = word
def findWords(self, board: List[List[str]], words: List[str]) -> List[str]:
for word in words:
self.insert(word)
self.output = []
m,n = len(board),len(board[0])
def search(node,i,j):
tmp = board[i][j]
if tmp not in node.children:
return
node = node.children[tmp]
if node.word != '':
self.output.append(node.word)
node.word = ''
board[i][j] = '#'
for i1,j1 in [(i-1,j), (i+1,j), (i,j+1), (i,j-1)]:
if 0<=i1<m and 0<=j1<n:
search(node,i1,j1)
board[i][j] = tmp
for i in range(m):
for j in range(n):
search(self.root,i,j)
return self.output