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:
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);
*/
运行代码
提交
javascript 解法, 执行用时: 143 ms, 内存消耗: 57.7 MB, 提交时间: 2024-08-12 09:17:58
var MagicDictionary = function() {
this.root = new Trie();
};
MagicDictionary.prototype.buildDict = function(dictionary) {
for (const word of dictionary) {
let cur = this.root;
for (let i = 0; i < word.length; ++i) {
const ch = word[i];
const idx = ch.charCodeAt() - 'a'.charCodeAt();
if (!cur.child[idx]) {
cur.child[idx] = new Trie();
}
cur = cur.child[idx];
}
cur.isFinished = true;
}
};
MagicDictionary.prototype.search = function(searchWord) {
return dfs(searchWord, this.root, 0, false);
};
const dfs = (searchWord, node, pos, modified) => {
if (pos === searchWord.length) {
return modified && node.isFinished;
}
let idx = searchWord[pos].charCodeAt() - 'a'.charCodeAt();
if (node.child[idx]) {
if (dfs(searchWord, node.child[idx], pos + 1, modified)) {
return true;
}
}
if (!modified) {
for (let i = 0; i < 26; ++i) {
if (i !== idx && node.child[i]) {
if (dfs(searchWord, node.child[i], pos + 1, true)) {
return true;
}
}
}
}
return false;
}
class Trie {
constructor() {
this.isFinished = false;
this.child = new Array(26).fill(0);
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* var obj = new MagicDictionary()
* obj.buildDict(dictionary)
* var param_2 = obj.search(searchWord)
*/
java 解法, 执行用时: 32 ms, 内存消耗: 44.9 MB, 提交时间: 2024-08-12 09:17:23
class MagicDictionary {
Trie root;
public MagicDictionary() {
root = new Trie();
}
public void buildDict(String[] dictionary) {
for (String word : dictionary) {
Trie cur = root;
for (int i = 0; i < word.length(); ++i) {
char ch = word.charAt(i);
int idx = ch - 'a';
if (cur.child[idx] == null) {
cur.child[idx] = new Trie();
}
cur = cur.child[idx];
}
cur.isFinished = true;
}
}
public boolean search(String searchWord) {
return dfs(searchWord, root, 0, false);
}
private boolean dfs(String searchWord, Trie node, int pos, boolean modified) {
if (pos == searchWord.length()) {
return modified && node.isFinished;
}
int idx = searchWord.charAt(pos) - 'a';
if (node.child[idx] != null) {
if (dfs(searchWord, node.child[idx], pos + 1, modified)) {
return true;
}
}
if (!modified) {
for (int i = 0; i < 26; ++i) {
if (i != idx && node.child[i] != null) {
if (dfs(searchWord, node.child[i], pos + 1, true)) {
return true;
}
}
}
}
return false;
}
}
class Trie {
boolean isFinished;
Trie[] child;
public Trie() {
isFinished = false;
child = new Trie[26];
}
}
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary obj = new MagicDictionary();
* obj.buildDict(dictionary);
* boolean param_2 = obj.search(searchWord);
*/
cpp 解法, 执行用时: 52 ms, 内存消耗: 97.3 MB, 提交时间: 2024-08-12 09:16:57
struct Trie {
bool is_finished;
Trie* child[26];
Trie() {
is_finished = false;
fill(begin(child), end(child), nullptr);
}
};
class MagicDictionary {
public:
MagicDictionary() {
root = new Trie();
}
void buildDict(vector<string> dictionary) {
for (auto&& word: dictionary) {
Trie* cur = root;
for (char ch: word) {
int idx = ch - 'a';
if (!cur->child[idx]) {
cur->child[idx] = new Trie();
}
cur = cur->child[idx];
}
cur->is_finished = true;
}
}
bool search(string searchWord) {
function<bool(Trie*, int, bool)> dfs = [&](Trie* node, int pos, bool modified) {
if (pos == searchWord.size()) {
return modified && node->is_finished;
}
int idx = searchWord[pos] - 'a';
if (node->child[idx]) {
if (dfs(node->child[idx], pos + 1, modified)) {
return true;
}
}
if (!modified) {
for (int i = 0; i < 26; ++i) {
if (i != idx && node->child[i]) {
if (dfs(node->child[i], pos + 1, true)) {
return true;
}
}
}
}
return false;
};
return dfs(root, 0, false);
}
private:
Trie* root;
};
/**
* Your MagicDictionary object will be instantiated and called as such:
* MagicDictionary* obj = new MagicDictionary();
* obj->buildDict(dictionary);
* bool param_2 = obj->search(searchWord);
*/
python3 解法, 执行用时: 288 ms, 内存消耗: 19.8 MB, 提交时间: 2022-11-22 09:51:37
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:51:09
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:38
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 解法, 执行用时: 68 ms, 内存消耗: 15.5 MB, 提交时间: 2022-11-22 09:47:43
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)