Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Week_04/id_15/LeetCode_208_015.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#Data Node to hold char data
class Node:
def __init__(self, val, isWord=False):
self.val = val
self.child = {}
self.isWord = isWord

class Trie:

def __init__(self):
"""
Initialize your data structure here.
"""
self.root = Node("")


def insert(self, word: str) -> None:
"""
Inserts a word into the trie.
"""
curr = self.root
for ch in word:
if ch not in curr.child:
curr.child[ch] = Node(ch)
curr = curr.child.get(ch)

curr.isWord = True


def search(self, word: str) -> bool:
"""
Returns if the word is in the trie.
"""
curr = self.root
for ch in word:
if ch in curr.child:
curr = curr.child.get(ch)
else:
return False

return curr.isWord

def startsWith(self, prefix: str) -> bool:
"""
Returns if there is any word in the trie that starts with the given prefix.
"""
curr = self.root
for ch in prefix:
if ch in curr.child:
curr = curr.child.get(ch)
else:
return False

return True
13 changes: 13 additions & 0 deletions Week_04/id_15/LeetCode_211_015.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class WordDictionary(object):
def __init__(self):
self.len2words = collections.defaultdict(list)

def addWord(self, word):
self.len2words[len(word)].append(word)

def search(self, word):
words = self.len2words[len(word)]
for i, char in enumerate(word):
words = [w for w in words if char in ('.', w[i])]
if not words: return False
return True
7 changes: 7 additions & 0 deletions Week_04/id_15/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
# 学习笔记

## 日常计划
- 完成相对应的算法习题
- 规划长期算法成长路线

## 下周计划
- 争取通过算法考试