C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class MagicDictionary {
public:
/** Initialize your data structure here. */
MagicDictionary() {
}
void buildDict(vector<string> dictionary) {
}
bool search(string searchWord) {
}
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary* obj = new MagicDictionary();
* obj->buildDict(dictionary);
* bool param_2 = obj->search(searchWord);
*/
运行代码
提交
python3 解法, 执行用时: 244 ms, 内存消耗: 19.8 MB, 提交时间: 2022-11-22 09:54:43
class Trie:
def __init__(self):
self.is_finished = False
self.child = dict()
class MagicDictionary:
def __init__(self):
self.root = Trie()
def buildDict(self, dictionary: List[str]) -> None:
for word in dictionary:
cur = self.root
for ch in word:
if ch not in cur.child:
cur.child[ch] = Trie()
cur = cur.child[ch]
cur.is_finished = True
def search(self, searchWord: str) -> bool:
def dfs(node: Trie, pos: int, modified: bool) -> bool:
if pos == len(searchWord):
return modified and node.is_finished
ch = searchWord[pos]
if ch in node.child:
if dfs(node.child[ch], pos + 1, modified):
return True
if not modified:
for cnext in node.child:
if ch != cnext:
if dfs(node.child[cnext], pos + 1, True):
return True
return False
return dfs(self.root, 0, False)
# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dictionary)
# param_2 = obj.search(searchWord)
golang 解法, 执行用时: 32 ms, 内存消耗: 8 MB, 提交时间: 2022-11-22 09:50:53
// 字典树
type trie struct {
children [26]*trie
isFinished bool
}
type MagicDictionary struct {
*trie
}
func Constructor() MagicDictionary {
return MagicDictionary{&trie{}}
}
func (d *MagicDictionary) BuildDict(dictionary []string) {
for _, word := range dictionary {
cur := d.trie
for _, c := range word {
c -= 'a'
if cur.children[c] == nil {
cur.children[c] = &trie{}
}
cur = cur.children[c]
}
cur.isFinished = true
}
}
func dfs(node *trie, searchWord string, modified bool) bool {
if searchWord == "" {
return modified && node.isFinished
}
c := searchWord[0] - 'a'
if node.children[c] != nil && dfs(node.children[c], searchWord[1:], modified) {
return true
}
if !modified {
for i, child := range node.children {
if i != int(c) && child != nil && dfs(child, searchWord[1:], true) {
return true
}
}
}
return false
}
func (d *MagicDictionary) Search(searchWord string) bool {
return dfs(d.trie, searchWord, false)
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.BuildDict(dictionary);
* param_2 := obj.Search(searchWord);
*/
golang 解法, 执行用时: 12 ms, 内存消耗: 6.7 MB, 提交时间: 2022-11-22 09:49:59
type MagicDictionary []string
func Constructor() MagicDictionary {
return MagicDictionary{}
}
func (d *MagicDictionary) BuildDict(dictionary []string) {
*d = dictionary
}
func (d *MagicDictionary) Search(searchWord string) bool {
next:
for _, word := range *d {
if len(word) != len(searchWord) {
continue
}
diff := false
for i := range word {
if word[i] != searchWord[i] {
if diff {
continue next
}
diff = true
}
}
if diff {
return true
}
}
return false
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* obj := Constructor();
* obj.BuildDict(dictionary);
* param_2 := obj.Search(searchWord);
*/
python3 解法, 执行用时: 76 ms, 内存消耗: 15.5 MB, 提交时间: 2022-11-22 09:47:22
class MagicDictionary:
def __init__(self):
self.words = list()
def buildDict(self, dictionary: List[str]) -> None:
self.words = dictionary
def search(self, searchWord: str) -> bool:
for word in self.words:
if len(word) != len(searchWord):
continue
diff = 0
for chx, chy in zip(word, searchWord):
if chx != chy:
diff += 1
if diff > 1:
break
if diff == 1:
return True
return False
# Your MagicDictionary object will be instantiated and called as such:
# obj = MagicDictionary()
# obj.buildDict(dictionary)
# param_2 = obj.search(searchWord)