Skip to content

Commit 9510f5d

Browse files
committed
:squirrel:
0 parents  commit 9510f5d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+940
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# General
2+
.DS_Store
3+
.AppleDouble
4+
.LSOverride
5+
6+
# Icon must end with two \r
7+
Icon
8+
9+
10+
# Thumbnails
11+
._*
12+
13+
# Files that might appear in the root of a volume
14+
.DocumentRevisions-V100
15+
.fseventsd
16+
.Spotlight-V100
17+
.TemporaryItems
18+
.Trashes
19+
.VolumeIcon.icns
20+
.com.apple.timemachine.donotpresent
21+
22+
# Directories potentially created on remote AFP share
23+
.AppleDB
24+
.AppleDesktop
25+
Network Trash Folder
26+
Temporary Items
27+
.apdisk

add-binary.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# https://leetcode.com/problems/add-binary/
2+
3+
class Solution:
4+
def addBinary(self, a: str, b: str) -> str:
5+
carry = 0
6+
ans = ''
7+
i, j = len(a)-1, len(b)-1
8+
while i >= 0 or j >= 0:
9+
n = carry
10+
if i >= 0:
11+
n += int(a[i])
12+
if j >= 0:
13+
n += int(b[j])
14+
15+
if n == 3:
16+
ans += '1'
17+
carry = 1
18+
elif n == 2:
19+
ans += '0'
20+
carry = 1
21+
elif n == 1:
22+
ans += '1'
23+
carry = 0
24+
elif n == 0:
25+
ans += '0'
26+
carry = 0
27+
i -= 1
28+
j -= 1
29+
if carry == 1:
30+
ans += '1'
31+
32+
return ans[::-1]

add-two-numbers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# https://leetcode.com/problems/add-two-numbers/
2+
3+
# Definition for singly-linked list.
4+
# class ListNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.next = None
8+
9+
class Solution:
10+
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
11+
carry = 0
12+
p = ListNode(-1)
13+
ans = p
14+
while l1 is not None or l2 is not None:
15+
aux = carry
16+
if l1 is not None:
17+
aux += l1.val
18+
l1 = l1.next
19+
if l2 is not None:
20+
aux += l2.val
21+
l2 = l2.next
22+
if aux > 9:
23+
carry = 1
24+
else:
25+
carry = 0
26+
aux_node = ListNode(aux%10)
27+
p.next = aux_node
28+
p = p.next
29+
if carry == 1:
30+
p.next = ListNode(1)
31+
return ans.next

alien-dictionary.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution:
2+
def alienOrder(self, words: List[str]) -> str:
3+
from collections import defaultdict
4+
5+
def build_graph(words):
6+
graph = defaultdict(set)
7+
indegree = dict()
8+
9+
for word in words:
10+
for c in word:
11+
indegree[c] = 0
12+
13+
for i in range(len(words)-1):
14+
w1, w2 = words[i], words[i+1]
15+
length = min(len(w1), len(w2))
16+
for j in range(length):
17+
c1, c2 = w1[j], w2[j]
18+
if c1 != c2:
19+
graph[c1].add(c2)
20+
break
21+
22+
for (u, neighbors,) in graph.items():
23+
for v in neighbors:
24+
indegree[v] += 1
25+
26+
return (graph, indegree,)
27+
28+
29+
def topological_traversal(graph):
30+
g = graph[0]
31+
indegree = graph[1]
32+
33+
ans = []
34+
queue = []
35+
36+
for (u, k,) in indegree.items():
37+
if k == 0:
38+
queue.append(u)
39+
40+
while queue:
41+
u = queue.pop(0)
42+
ans.append(u)
43+
for v in g[u]:
44+
indegree[v] -= 1
45+
if indegree[v] == 0:
46+
queue.append(v)
47+
48+
return ''.join(ans) if len(ans) == len(indegree) else ''
49+
50+
g = build_graph(words)
51+
return topological_traversal(g)

backspace-string-compare.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://leetcode.com/problems/backspace-string-compare/
2+
3+
class Solution:
4+
def backspaceCompare(self, S: str, T: str) -> bool:
5+
stack1, stack2 = [], []
6+
i, j = 0, 0
7+
n1, n2, = len(S), len(T)
8+
9+
while i<n1:
10+
if S[i] == '#':
11+
if stack1:
12+
stack1.pop()
13+
else:
14+
stack1.append(S[i])
15+
i += 1
16+
17+
while j<n2:
18+
if T[j] == '#':
19+
if stack2:
20+
stack2.pop()
21+
else:
22+
stack2.append(T[j])
23+
j += 1
24+
25+
return stack1 == stack2

binary-search-tree-iterator.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# https://leetcode.com/problems/binary-search-tree-iterator/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.left = None
8+
# self.right = None
9+
10+
class BSTIterator:
11+
12+
def __init__(self, root: TreeNode):
13+
self.stack = []
14+
self._left_most_node(root)
15+
16+
def _left_most_node(self, node: TreeNode):
17+
while node is not None:
18+
self.stack.append(node)
19+
node = node.left
20+
21+
def next(self) -> int:
22+
"""
23+
@return the next smallest number
24+
"""
25+
aux = self.stack.pop()
26+
if aux.right is not None:
27+
self._left_most_node(aux.right)
28+
return aux.val
29+
30+
def hasNext(self) -> bool:
31+
"""
32+
@return whether we have a next smallest number
33+
"""
34+
return len(self.stack) > 0
35+
36+
37+
38+
# Your BSTIterator object will be instantiated and called as such:
39+
# obj = BSTIterator(root)
40+
# param_1 = obj.next()
41+
# param_2 = obj.hasNext()

combination-sum-ii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# https://leetcode.com/problems/combination-sum-ii/
2+
3+
class Solution:
4+
def combinationSum2(self, candidates: List[int], target: int) -> List[List[int]]:
5+
def backtrack(l, n, idx, aux, current_sum, k, ans):
6+
if current_sum > k:
7+
return
8+
if current_sum == k:
9+
ans.add(tuple(sorted(aux)))
10+
return
11+
for i in range(idx, n):
12+
aux.append(l[i])
13+
backtrack(l, n, i+1, aux, current_sum+l[i], k, ans)
14+
aux.pop()
15+
16+
ans = set()
17+
backtrack(candidates, len(candidates), 0, [], 0, target, ans)
18+
return list(ans)

confusing-number.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# https://leetcode.com/problems/confusing-number/
2+
3+
class Solution:
4+
def confusingNumber(self, N: int) -> bool:
5+
invalid = ['2','3','4','5','7']
6+
s = str(N)
7+
for x in s:
8+
if x in invalid:
9+
return False
10+
m = dict()
11+
m['0'] = '0'
12+
m['1'] = '1'
13+
m['6'] = '9'
14+
m['8'] = '8'
15+
m['9'] = '6'
16+
aux = ''
17+
for x in s[::-1]:
18+
aux += m[x]
19+
return s != aux

design-phone-directory.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# https://leetcode.com/problems/design-phone-directory/
2+
3+
class PhoneDirectory:
4+
5+
def __init__(self, maxNumbers: int):
6+
"""
7+
Initialize your data structure here
8+
@param maxNumbers - The maximum numbers that can be stored in the phone directory.
9+
"""
10+
self.occupied_numbers = set()
11+
self.not_occupied_numbers = { x for x in range(maxNumbers) }
12+
13+
14+
def get(self) -> int:
15+
"""
16+
Provide a number which is not assigned to anyone.
17+
@return - Return an available number. Return -1 if none is available.
18+
"""
19+
number = -1
20+
for x in self.not_occupied_numbers:
21+
number = x
22+
if number != -1:
23+
self.occupied_numbers.add(number)
24+
self.not_occupied_numbers.remove(number)
25+
return number
26+
27+
28+
def check(self, number: int) -> bool:
29+
"""
30+
Check if a number is available or not.
31+
"""
32+
return number in self.not_occupied_numbers
33+
34+
35+
def release(self, number: int) -> None:
36+
"""
37+
Recycle or release a number.
38+
"""
39+
if number in self.occupied_numbers:
40+
self.occupied_numbers.remove(number)
41+
self.not_occupied_numbers.add(number)

diameter-of-binary-tree.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://leetcode.com/problems/diameter-of-binary-tree/
2+
3+
# Definition for a binary tree node.
4+
# class TreeNode:
5+
# def __init__(self, x):
6+
# self.val = x
7+
# self.left = None
8+
# self.right = None
9+
10+
class Solution:
11+
def diameterOfBinaryTree(self, root: TreeNode) -> int:
12+
if root is None:
13+
return 0
14+
self.ans = 0
15+
def f(v):
16+
if v is None:
17+
return 0
18+
l_depth = f(v.left)
19+
r_depth = f(v.right)
20+
self.ans = max(self.ans, l_depth + r_depth + 1)
21+
return max(l_depth, r_depth) + 1
22+
f(root)
23+
return self.ans-1

0 commit comments

Comments
 (0)