Skip to content

Instantly share code, notes, and snippets.

@hisea
Created October 19, 2020 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hisea/15094680d222ee2f0eb0226f11339bb9 to your computer and use it in GitHub Desktop.
Save hisea/15094680d222ee2f0eb0226f11339bb9 to your computer and use it in GitHub Desktop.
LeetCode Template
# Recursion
def recursion(level, param1, param2):
# recursion terminator
if level > MAX_LEVEL:
print_result
return
# process logic in current level
process_data(level, data)
# drill down
self.recursion(level + 1, p1, p2)
# reverse the current level status if needed
reverse_state(level)
#DFS
visited = set()
def dfs(node, visited):
visited.add(node)
# process current node here
# print(node)
for next_node in node.children():
if not next_node in visited:
dfs(next_node, visited)
# BFS
def bfs(graph, start, end):
queue = []
queue.append([start])
visited.add(start)
while queue:
node = queue.pop()
visited.add(node)
process(node)
nodes = generate_related_nodes(node)
queue.push(nodes)
# Binary Search
left, right = 0, len(arr) -1
while left <= right:
mid = left + (right - left) / 2
if arr[mid] == target:
break or return result
elif array[mid] < target:
left = mid + 1
else:
right = mid - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment