Skip to content

Commit 52ac606

Browse files
committed
Partial Old questions
1 parent d94340e commit 52ac606

9 files changed

+214
-0
lines changed

linkedlist/148. Sort List - EricD.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#iteration:
2+
def sortList(self, head):
3+
"""
4+
:type head: ListNode
5+
:rtype: ListNode
6+
"""
7+
if (not head) or (not head.next):
8+
return head
9+
slow = fast = head
10+
while fast.next and fast.next.next:
11+
fast = fast.next.next
12+
slow = slow.next
13+
14+
p1 = self.sortList(slow.next)
15+
slow.next = None
16+
p2 = self.sortList(head)
17+
18+
p = res = ListNode(None)
19+
while p1 and p2:
20+
if p1.val<p2.val:
21+
p.next = p1
22+
p1 = p1.next
23+
p = p.next
24+
else:
25+
p.next = p2
26+
p2 = p2.next
27+
p = p.next
28+
if p1:
29+
p.next = p1
30+
if p2:
31+
p.next = p2
32+
return res.next
33+
34+
#array...
35+
def sortList(self, head):
36+
"""
37+
:type head: ListNode
38+
:rtype: ListNode
39+
"""
40+
items = []
41+
while head:
42+
items.append(head.val)
43+
head = head.next
44+
items.sort()
45+
p = res = ListNode(None)
46+
for i in items:
47+
p.next = ListNode(i)
48+
p = p.next
49+
return res.next
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def addTwoNumbers(self, l1, l2):
2+
"""
3+
:type l1: ListNode
4+
:type l2: ListNode
5+
:rtype: ListNode
6+
"""
7+
l3 = res = ListNode(None)
8+
p=0
9+
while l1 and l2:
10+
n,p = (l1.val+l2.val+p)%10,(l1.val+l2.val+p)/10
11+
l3.next = ListNode(n)
12+
l3 = l3.next
13+
l1 = l1.next
14+
l2 = l2.next
15+
while l1:
16+
n,p = (l1.val+p)%10,(l1.val+p)/10
17+
l3.next = ListNode(n)
18+
l3 = l3.next
19+
l1 = l1.next
20+
while l2:
21+
n,p = (l2.val+p)%10,(l2.val+p)/10
22+
l3.next = ListNode(n)
23+
l3 = l3.next
24+
l2 = l2.next
25+
if p>0:
26+
l3.next = ListNode(p)
27+
return res.next
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def addTwoNumbers(self, l1, l2):
2+
"""
3+
:type l1: ListNode
4+
:type l2: ListNode
5+
:rtype: ListNode
6+
"""
7+
n1,n2=0,0
8+
while l1:
9+
n1 = n1*10+l1.val
10+
l1 = l1.next
11+
while l2:
12+
n2 = n2*10+l2.val
13+
l2 = l2.next
14+
15+
sum = str(n1+n2)
16+
cur = res = ListNode(None)
17+
for n in sum:
18+
cur.next =ListNode(n)
19+
cur = cur.next
20+
return res.next

tree/EricD/100. Same Tree - EricD.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def isSameTree(self, p, q):
2+
"""
3+
:type p: TreeNode
4+
:type q: TreeNode
5+
:rtype: bool
6+
"""
7+
if p is None and q is None:
8+
return True
9+
elif not (p and q):
10+
return False
11+
else:
12+
return (p.val == q.val) and self.isSameTree(p.left, q.left) and self.isSameTree(p.right,q.right)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def maxDepth(self, root):
2+
"""
3+
:type root: TreeNode
4+
:rtype: int
5+
"""
6+
return max(self.maxDepth(root.left),self.maxDepth(root.right))+1 if root else 0
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#DFS
2+
def minDepth(self, root):
3+
"""
4+
:type root: TreeNode
5+
:rtype: int
6+
"""
7+
if root:
8+
if root.left is None and root.right is None:
9+
return 1
10+
else:
11+
l = self.minDepth(root.left) if root.left else sys.maxint
12+
r = self.minDepth(root.right) if root.right else sys.maxint
13+
return min(l,r)+1
14+
15+
else:
16+
return 0
17+
18+
#BFS
19+
def minDepth(self, root):
20+
"""
21+
:type root: TreeNode
22+
:rtype: int
23+
"""
24+
if root:
25+
level = 0
26+
cur = [root]
27+
while cur:
28+
level+=1
29+
nextR = []
30+
for node in cur:
31+
if node.left is None and node.right is None:
32+
return level
33+
if node.left: nextR.append(node.left)
34+
if node.right: nextR.append(node.right)
35+
cur = nextR
36+
else:
37+
return 0
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Binary Search Tree!
2+
def lowestCommonAncestor(self, root, p, q):
3+
"""
4+
:type root: TreeNode
5+
:type p: TreeNode
6+
:type q: TreeNode
7+
:rtype: TreeNode
8+
"""
9+
while (root.val-p.val)*(root.val-q.val)>0:
10+
root = root.left if root.val>q.val else root.right
11+
return root
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def decodeString(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
stack = []
8+
for i in s:
9+
if i != ']':
10+
stack.append(i)
11+
else:
12+
word = []
13+
while stack[-1] != '[':
14+
word.append(stack.pop())
15+
stack.pop()
16+
freq = []
17+
while stack and stack[-1].isdigit():
18+
freq.append(stack.pop())
19+
k=''.join(freq[::-1])
20+
w = ''.join(word[::-1])
21+
stack.append(w*int(k))
22+
23+
return ''.join(stack)
24+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def __init__(self):
10+
self.sum=0
11+
12+
def convertBST(self, root):
13+
"""
14+
:type root: TreeNode
15+
:rtype: TreeNode
16+
"""
17+
k=[0]
18+
19+
if not root:
20+
return root
21+
if root.right:
22+
self.convertBST(root.right)
23+
root.val+=self.sum
24+
self.sum=root.val
25+
if root.left:
26+
self.convertBST(root.left)
27+
return root
28+

0 commit comments

Comments
 (0)